【POJ 2516】 Minimum Cost (最小费)
Minimum Cost
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 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
POJ Monthly--2005.07.31, Wang Yijie
|
花费以k个矩阵表示 第z个矩阵里元素ai,j表示单位商品z由j运到i的花费
这样就变成k个图 代表每件物品的运送花费
每个个图一个超级源点一个超级汇点
超级源点连接每个供货商 花费0流量为供货商供应该物品的量
每个店主连接超级汇点 花费0流量为店主对该物品的需求量
每个店主连接每个供货商 花费为矩阵对应花费 流量为店主或供应商流量均可(最大流量已经被超源跟超汇限制 只要大于最小值即可)
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; struct Edge { Edge(){} int v,cost,cup,next; Edge(int _v,int _cost,int _cup,int _next):v(_v),cost(_cost),cup(_cup),next(_next){} }; //建图 Edge eg[20100]; int head[233]; int tp; //跑最小费用最大流 int dis[233],pre[233]; bool vis[233]; //读数据 int ned[55][55],has[55][55]; int nsum[55],hsum[55]; int matrix[55][55][55]; int n,m,k,st,en,cost; void init() { memset(nsum,0,sizeof(nsum)); memset(hsum,0,sizeof(hsum)); st = 0; en = n+m+1; } void Add(int u,int v,int cost,int cup) { eg[tp] = Edge(v,cost,cup,head[u]); head[u] = tp++; eg[tp] = Edge(u,-cost,0,head[v]); head[v] = tp++; } bool spfa() { memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); pre[st] = pre[en] = -1; queue <int> q; q.push(st); vis[st] = 1; dis[st] = 0; int minflow = INF; while(!q.empty()) { int u = q.front(); vis[u] = 0; q.pop(); for(int i = head[u]; i != -1; i = eg[i].next) { int v = eg[i].v; if(dis[v] > dis[u]+eg[i].cost && eg[i].cup) { dis[v] = dis[u]+eg[i].cost; minflow = min(minflow,eg[i].cup); pre[v] = i; if(!vis[v]) { q.push(v); vis[v] = 1; } } } } if(pre[en] == -1) return 0; cost += dis[en]; for(int i = pre[en]; i != -1; i = pre[eg[i^1].v]) { eg[i].cup -= minflow; eg[i^1].cup += minflow; } return 1; } int main() { while(~scanf("%d %d %d",&n,&m,&k) && (n+m+k)) { init(); //记录每个店长对每件物品的需求量 for(int i = 1; i <= n; ++i) for(int j = 1; j <= k; ++j) { scanf("%d",&ned[i][j]); nsum[j] += ned[i][j]; } //记录每个供货商对每件物品的供应量 for(int i = 1; i <= m; ++i) for(int j = 1; j <= k; ++j) { scanf("%d",&has[i][j]); hsum[j] += has[i][j]; } //记录运送花费 for(int i = 1; i <= k; ++i) for(int j = 1; j <= n; ++j) for(int l = 1; l <= m; ++l) scanf("%d",&matrix[i][j][l]); //判断是否可行 即供满足求 bool f = 1; for(int i = 1; i <= k; ++i) { if(hsum[i] < nsum[i]) { f = 0; break; } } if(!f) { puts("-1"); continue; } //对于每件物品建图跑最小费 cost = 0; for(int z = 1; z <= k; ++z) { tp = 0; memset(head,-1,sizeof(head)); for(int i = 1; i <= n; ++i) Add(st,i,0,ned[i][z]); for(int i = n+1; i < en; ++i) Add(i,en,0,has[i-n][z]); for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) Add(i,n+j,matrix[z][i][j],min(ned[i][z],has[j][z])); while(spfa()); } printf("%d\n",cost); } return 0; }