Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 10978 | Accepted: 3692 |
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
Sample Output
4 -1
Source
POJ Monthly--2005.07.31, Wang Yijie
很经典的最小费用最大流的题目。
就是有K种商品,用K次最小费用最大流。
看程序吧,不解释。
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<queue> using namespace std; //************************************************************ //最小费用最大流算法 //SPFA求最短路 //邻接矩阵形式 //初始化:cap:容量,没有边为0 //cost:耗费,对称形式,没有边的也为0 //c是最小费用 //f是最大流 //******************************************************* const int MAXN=200; const int INF=0x3fffffff; int cap[MAXN][MAXN];//容量,没有边为0 int flow[MAXN][MAXN]; //耗费矩阵是对称的,有i到j的费用,则j到i的费用为其相反数 int cost[MAXN][MAXN]; int n;//顶点数目0~n-1 int f;//最大流 int c;//最小费用 int start,end;//源点和汇点 bool vis[MAXN];//在队列标志 int que[MAXN]; int pre[MAXN]; int dist[MAXN];//s-t路径最小耗费 bool SPFA() { int front=0,rear=0; for(int u=0;u<=n;u++) { if(u==start) { que[rear++]=u; dist[u]=0; vis[u]=true; } else { dist[u]=INF; vis[u]=false; } } while(front!=rear) { int u=que[front++]; vis[u]=false; if(front>=MAXN)front=0; for(int v=0;v<=n;v++) { if(cap[u][v]>flow[u][v]&&dist[v]>dist[u]+cost[u][v]) { dist[v]=dist[u]+cost[u][v]; pre[v]=u; if(!vis[v]) { vis[v]=true; que[rear++]=v; if(rear>=MAXN)rear=0; } } } } if(dist[end]>=INF)return false; return true; } void minCostMaxflow() { memset(flow,0,sizeof(flow)); c=f=0; while(SPFA()) { int Min=INF; for(int u=end;u!=start;u=pre[u]) Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]); for(int u=end;u!=start;u=pre[u]) { flow[pre[u]][u]+=Min; flow[u][pre[u]]-=Min; } c+=dist[end]*Min; f+=Min; } } //************************************************************ int order[MAXN][MAXN]; int supply[MAXN][MAXN]; int tolorder[MAXN]; int main() { //freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int N,M; int K; while(scanf("%d%d%d",&N,&M,&K)!=EOF) { if(N==0&&M==0&&K==0)break; start=0; n=N+M+1; end=n; memset(cap,0,sizeof(cap)); memset(cost,0,sizeof(cost)); for(int i=0;i<N;i++) for(int j=0;j<K;j++) scanf("%d",&order[i][j]); for(int i=0;i<M;i++) for(int j=0;j<K;j++) scanf("%d",&supply[i][j]); for(int i=0;i<K;i++) { tolorder[i]=0; for(int j=0;j<N;j++) tolorder[i]+=order[j][i]; } for(int i=1;i<=M;i++) for(int j=M+1;j<=N+M;j++) cap[i][j]=INF; bool flag=true; int ans=0; for(int i=0;i<K;i++) { for(int j=M+1;j<=M+N;j++) for(int k=1;k<=M;k++) { scanf("%d",&cost[k][j]); cost[j][k]=-cost[k][j]; } if(!flag)continue; for(int j=1;j<=M;j++) cap[start][j]=supply[j-1][i]; for(int j=1;j<=N;j++) cap[j+M][end]=order[j-1][i]; /* for(int k=0;k<=n;k++) { for(int j=0;j<=n;j++) printf("%d ",cap[k][j]); printf("\n"); } */ minCostMaxflow(); // printf("%d %d\n",f,c); if(f<tolorder[i])flag=false; else ans+=c; } if(!flag)printf("-1\n"); else printf("%d\n",ans); } return 0; }