Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 16168 | Accepted: 5672 |
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.
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.
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
终于写了一道最小费用最大流问题的题目了。很不容易啊!
题意:有N个店家、M个仓库以及K种物品。接下来N行每行K个数,表示每个店家对每一种物品的需求。后面跟着M行,表示每个仓库所存储的每种物品的数目。
最后给出K个N*M的矩阵。对于第k个矩阵,它的第i行第j列表示——第j个仓库运送第k种物品到第i个店家的费用。现在问你能否满足所有店家的需求,若可以输出最小的花费,反之输出-1。
代码:
#include <cstring> #include <cstdio> #include <algorithm> #include <iostream> #include <queue> using namespace std; const int Ma = 10052; const int Me = 201; const int inf = 202020202; struct node { int from, to, cap; int flow, cost, next; }E[Ma]; int head[Me], top; int dist[Me], pre[Me]; bool vis[Me], flag; int source, sink; int N, M, K; int need[60][60], have[60][60]; int Sneed[60], Shave[60]; int used[60][60]; void init() { top = 0; memset ( head, -1, sizeof(head) ); } void AddEdge(int u, int v, int w, int c) { node temp = {u, v, w, 0, c, head[u]}; E[top] = temp; head[u] = top++; node temp1 = {v, u, 0, 0, -c, head[v]}; E[top] = temp1; head[v] = top++; } bool SPFA(int s, int t) { int i; queue<int> q; memset ( dist, inf, sizeof(dist) ); memset ( vis, false, sizeof(vis) ); memset ( pre, -1, sizeof(pre) ); dist[s] = 0; vis[s] = true; q.push(s); while ( !q.empty() ) { int u = q.front(); q.pop(); vis[u] = false; for ( i = head[u];i != -1; i = E[i].next ) { node temp = E[i]; if ( dist[temp.to] > dist[u]+temp.cost&&temp.cap > temp.flow ) { dist[temp.to] = dist[u]+temp.cost; pre[temp.to] = i; if ( !vis[temp.to] ) { vis[temp.to] = true; q.push(temp.to); } } } } return pre[t] != -1; } void MCMF(int s, int t, int &cost) { cost = 0; while(SPFA(s, t)) { int Min = inf; for(int i = pre[t]; i != -1; i = pre[E[i^1].to]) { node temp = E[i]; Min = min(Min, temp.cap-temp.flow); } for(int i = pre[t]; i != -1; i = pre[E[i^1].to]) { E[i].flow = E[i].flow+Min; E[i^1].flow = E[i^1].flow-Min; cost = cost+E[i].cost * Min; } } } void solve() { int i, j, k; memset ( Sneed, 0, sizeof(Sneed) ); memset ( Shave, 0, sizeof(Shave) ); for ( i = 1;i <= N; i++ ) for ( j = 1;j <= K; j++ ) { scanf ( "%d", &need[i][j] ); Sneed[j] = Sneed[j]+need[i][j]; } for ( i = 1;i <= M; i++ ) for ( j = 1;j <= K; j++ ) { scanf ( "%d", &have[i][j] ); Shave[j] = Shave[j]+have[i][j]; } flag = true; for ( i = 1;i <= K; i++ ) if ( Shave[i] < Sneed[i] ) { flag = false; break; } int ans = 0; int cost = 0; for ( k = 1;k <= K; k++ ) { for ( i = 1;i <= N; i++ ) { for ( j = 1;j <= M; j++ ) scanf ( "%d", &used[i][j] ); } if ( !flag ) continue; init(); source = 0, sink = M+N+1; for ( i = 1;i <= N; i++ ) AddEdge(i, sink, need[i][k], 0); for ( i = 1;i <= M; i++ ) AddEdge(source, i+N, have[i][k], 0); for ( i = 1;i <= M; i++ ) for ( j = 1;j <= N; j++ ) AddEdge(i+N, j, inf, used[j][i]); MCMF(source, sink, cost); ans = ans+cost; } if ( flag ) printf ( "%d\n", ans ); else printf ( "-1\n" ); } int main() { while ( ~scanf ( "%d %d %d", &N, &M, &K )&&(N||M||K) ) { solve(); } return 0; }
代码菜鸟,如有错误,请多包涵!!!如有帮助记得支持我一下,谢谢!!!