[网络流24题] 19 负载平衡(最小代价供求,最小费用最大流)
2015-08-06 15:02
726 查看
题目大意;
G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等。搬运货物时,只能在相邻的仓库之间搬运。给定的n 个环形排列的仓库的库存量,计算使n 个仓库的库存数量相同的最少搬运量。
思路分析:
使n个仓库存储的货物数量相同,这个数量也就是n个仓库存储的货物数量的平均值,那么:
①:对于货物数量高于平均值的仓库,也就是需要搬出货物,就让这个仓库与源点相连,容量为这个仓库的货物数量与平均值的差值,表示有这么多的货物需要搬出去,费用为0;
②:对于货物数量低于平均值的仓库,也就是需要搬进货物,就让这个仓库与汇点相连,容量为平均值与这个仓库的货物数量的差值,表示有这么多的货物需要搬进来,费用为0;
③:让每个仓库都向它相邻的仓库连一条边,容量为INF,费用为1;
代码实现:
也可以如此建图:
G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等。搬运货物时,只能在相邻的仓库之间搬运。给定的n 个环形排列的仓库的库存量,计算使n 个仓库的库存数量相同的最少搬运量。
思路分析:
使n个仓库存储的货物数量相同,这个数量也就是n个仓库存储的货物数量的平均值,那么:
①:对于货物数量高于平均值的仓库,也就是需要搬出货物,就让这个仓库与源点相连,容量为这个仓库的货物数量与平均值的差值,表示有这么多的货物需要搬出去,费用为0;
②:对于货物数量低于平均值的仓库,也就是需要搬进货物,就让这个仓库与汇点相连,容量为平均值与这个仓库的货物数量的差值,表示有这么多的货物需要搬进来,费用为0;
③:让每个仓库都向它相邻的仓库连一条边,容量为INF,费用为1;
代码实现:
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) using namespace std; const int N=110, M=20410, INF=0x3f3f3f3f; int n, m, s, t, top, tp, sum_cost; int head , vis , dis , path , minflow , pre , ware ; struct Edge{ int to, next, flow, cost; Edge(int _to=0, int _next=0, int _flow=0, int _cost=0):to(_to),next(_next),flow(_flow),cost(_cost){} }edge[M]; void Addedge(int from, int to, int flow, int cost){ edge[top] = Edge(to, head[from], flow, cost); head[from] = top++; edge[top] = Edge(from, head[to], 0, -cost); head[to] = top++; } int Spfa(){ queue<int> q; memset(dis, 0x3f, sizeof(dis)); memset(minflow, 0x3f, sizeof(minflow)); memset(path, -1, sizeof(path)); memset(vis, 0, sizeof(vis)); dis[s] = 0; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i+1; i = edge[i].next){ if(edge[i].flow && dis[edge[i].to] > dis[u] + edge[i].cost){ dis[edge[i].to] = dis[u] + edge[i].cost; pre[edge[i].to] = u; path[edge[i].to] = i; minflow[edge[i].to] = Min(minflow[u], edge[i].flow); if(!vis[edge[i].to]){ vis[edge[i].to] = 1; q.push(edge[i].to); } } } } if(dis[t] == INF) return 0; sum_cost += minflow[t]*dis[t]; int u = t; while(u!=s){ edge[path[u]].flow -= minflow[t]; edge[path[u]^1].flow += minflow[t]; u = pre[u]; } return 1; } int main(){ freopen("overload.in", "r", stdin); freopen("overload.out", "w", stdout); scanf("%d", &n); memset(head, -1, sizeof(head)); s = top = sum_cost = 0; t = n + 1; int ave = 0, va; for(int i = 1; i <= n; ++i) scanf("%d", &ware[i]), ave += ware[i]; ave /= n; for(int i = 1; i < n; ++i){ Addedge(s, i, ware[i], 0); Addedge(i, t, ave, 0); Addedge(i, i+1, INF, 1); Addedge(i+1, i, INF, 1); } Addedge(s, n, ware , 0); Addedge(n, t, ave, 0); Addedge(1, n, INF, 1); Addedge(n, 1, INF, 1); while(Spfa()); printf("%d\n", sum_cost); }
也可以如此建图:
for(int i = 1; i < n; ++i){ Addedge(s, i, ware[i], 0); Addedge(i, t, ave, 0); Addedge(i, i+1, INF, 1); Addedge(i+1, i, INF, 1); } Addedge(s, n, ware , 0); Addedge(n, t, ave, 0); Addedge(1, n, INF, 1); Addedge(n, 1, INF, 1);
相关文章推荐
- Using Android Debug Bridge (ADB) via TCP/IP
- JAVA网络编程【转】出处不详
- Spring HTTP Invoker-远程调用模式
- tar命令(转自http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html)
- 【python网络编程】TCP套接字
- 【web】HTTP头 中ETag
- 性能分析:处理器、磁盘I/O、进程、网络分析方法 http://www.cnblogs.com/fnng/archive/2012/10/30/2747246.html
- AFNetWorking 学习
- XCode7beta4 ios9 webview https新特性
- Ajax--------XmlHttpRequest
- HTTP报文 http://www.cnblogs.com/kissdodog/archive/2013/04/01/2993228.html
- 高性能网络编程(1)—accept建立连接(转载,作者:陶辉)
- [网络流24题] 18 分配问题(二分图最佳匹配,最小费用最大流)
- TCP11种状态分析和测试
- 【Android应用开发技术:网络通信】网络服务可发现应用接口
- 【Android应用开发技术:网络通信】网络服务可发现使用方法
- 【Android应用开发技术:网络通信】网络服务可发现基本原理
- 人证比对TCPSERVER
- linux网络编程 no route to host 解决方案
- iOS iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)