poj2516Minimum Cost最小费用最大流
2016-02-21 10:13
281 查看
Description
Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods
(marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.
It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to
transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers' orders, with each line containing K integers (there integers are
belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places' storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in
that supply place.
Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.
The input is terminated with three "0"s. This test case should not be processed.
Output
For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".
Sample Input
Sample Output
Source
/*
* 把k种物品分开算 再求和
* 和EK最大流算法差不多,只不过EK算法是BFS搜索深度最小的可行路径作为增广路,而最小费用流是找权值最小的可行路径作为增广
* 路。(这里可行路径指可以使流量增大的路径)
* 这个代码完全是根据POJ1459改的,我把1459的代码贴在最下面了,就不讲了,还是画图,看代码才能理解。
*
*顺便说一下,有人说不知道增广路是什么,其实他啥也不是,在图论中,增广路就是能使结果更优的路径!
*二分图中增广路是可以使匹配数更大的路径,最大流中增广路是使流量更大的路径,最小费用流中是使流量满足条件并使费用尽量小的路径
*/
Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods
(marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.
It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to
transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers' orders, with each line containing K integers (there integers are
belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places' storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in
that supply place.
Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.
The input is terminated with three "0"s. This test case should not be processed.
Output
For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".
Sample Input
1 3 3 1 1 1 0 1 1 1 2 2 1 0 1 1 2 3 1 1 1 2 1 1 1 1 1 3 2 20 0 0 0
Sample Output
4 -1
Source
/*
* 把k种物品分开算 再求和
* 和EK最大流算法差不多,只不过EK算法是BFS搜索深度最小的可行路径作为增广路,而最小费用流是找权值最小的可行路径作为增广
* 路。(这里可行路径指可以使流量增大的路径)
* 这个代码完全是根据POJ1459改的,我把1459的代码贴在最下面了,就不讲了,还是画图,看代码才能理解。
*
*顺便说一下,有人说不知道增广路是什么,其实他啥也不是,在图论中,增广路就是能使结果更优的路径!
*二分图中增广路是可以使匹配数更大的路径,最大流中增广路是使流量更大的路径,最小费用流中是使流量满足条件并使费用尽量小的路径
*/
//poj2516 #include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <climits> #define maxn 55 #define MAX INT_MAX-11111111 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; int shopkeeper[maxn][maxn]; int storage[maxn][maxn]; int _map[maxn<<1][maxn<<1]; int capacity[maxn<<1][maxn<<1]; int prev[maxn<<1]; int flow[maxn<<1]; int shortest_dis[maxn<<1]; bool vis[maxn<<1]; int find_augment_path(int srt,int end,int n) { int i; for(i=0;i<n;++i) { shortest_dis[i]=MAX; } shortest_dis[srt]=0; mem(prev,-1); mem(vis,0); flow[end]=INT_MAX; queue<int>que; que.push(srt); while(!que.empty())//spfa 突然发现和bfs好像 { int now=que.front(); que.pop(); int i; for(i=0;i<n;++i) { if(capacity[now][i]>0 && shortest_dis[i]>shortest_dis[now]+_map[now][i])//流量为0的路不能走 相当于无穷大 { shortest_dis[i]=shortest_dis[now]+_map[now][i]; prev[i]=now; if(!vis[i]) { vis[i]=true; que.push(i); } } } vis[now]=false; } if(prev[end]!=-1) { int k=end; while(prev[k]!=-1) { flow[prev[k]]=min(flow[k],capacity[prev[k]][k]); k=prev[k]; } return flow[k];//flow[srt]; } return -1; } int MaxflowEK(int srt,int end,int n) { int up; int sum_flow=0; while((up=find_augment_path(srt,end,n))!=-1) { int k=end; int price_up=0; while(k!=srt) { int prev_node=prev[k]; capacity[prev_node][k]-=up; capacity[k][prev_node]+=up; price_up+=_map[prev[k]][k]*up; k=prev[k]; } sum_flow+=price_up; } return sum_flow; } int main() { int num_shopkeeper,num_storage,num_kind_of_goods; while(scanf("%d%d%d",&num_shopkeeper,&num_storage,&num_kind_of_goods),num_storage+num_shopkeeper+num_kind_of_goods) { int i,j; for(i=0;i<num_shopkeeper;++i) { for(j=0;j<num_kind_of_goods;++j) { scanf("%d",&shopkeeper[i][j]); } } for(i=0;i<num_storage;++i) { for(j=0;j<num_kind_of_goods;++j) { scanf("%d",&storage[i][j]); } } int ans=0; for(int k=0;k<num_kind_of_goods;++k) { mem(capacity,0); mem(_map,0); int temp_storage=0; for(i=0;i<num_storage;++i) { capacity[0][i+1]=storage[i][k]; temp_storage+=storage[i][k]; } for(i=0;i<num_shopkeeper;++i) { capacity[i+num_storage+1][num_shopkeeper+num_storage+1]=shopkeeper[i][k]; temp_storage-=shopkeeper[i][k]; } for(i=1;i<=num_storage;++i) { for(j=1;j<=num_shopkeeper;++j) { capacity[i][j+num_storage]=storage[i-1][k]; } } if(temp_storage<0) { ans=-1; } for(i=0;i<num_shopkeeper;++i) { for(j=0;j<num_storage;++j) { scanf("%d",&_map[j+1][i+1+num_storage]); _map[i+1+num_storage][j+1]=-_map[j+1][i+1+num_storage]; } } if(ans!=-1)//已经不可完成 不再计算 ans+=MaxflowEK(0,num_storage+num_shopkeeper+1,num_storage+num_shopkeeper+2); } printf("%d\n",ans); } }
相关文章推荐
- Segmentation in Hardware
- xib基础教程,手把手教你搭建炫酷的界面
- jCanvaScript canvas的操作库
- 我下载的jeecsm插件的一些资源
- 【VB.NET】获取文件版本信息
- 仿酷狗音乐播放器开发日志二十一 开发动态调色板控件(附源代码)
- [FMS]FMS5.0流媒体服务器配置文件Application.xml介绍
- 获取鼠标在屏幕的位置
- CodeForces 630 H. Benches(组合数学)
- Android多线程线程池(创建四种线程池)
- exec
- Linux memcached
- javaScript与MVC
- System.currentTimeMillis() :JAVA获取当前时间
- Android使用webView和ServerSocket模拟客户端和服务端通讯
- spring拦截器配置
- JavaWeb开发尝试
- 2015年Android开发新技术小结
- curl 命令
- exec , xargs