poj 2516(最小费用最大流)
其实题意很明确,最小费用最大流, 但是我这2货就建图就太二了, 我把所有的情况都弄到一个图里面。 总的点数有5000个,加上这么多的边,果断TLE。。。
后面知道第k个的情况是独立的,所以可以分成K次建图。 然后每次的点为100, 这样复杂度会小很多很多。 以后注意了,不要那么二的全部都在一起。 要坚持点边越少越好。
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
|
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; #define N 5050 #define INF 0x3ffffff struct node { int to,next,w,c; }edge[N*100]; int n,m,k; int s,t; int sn[N][N],sm[N][N]; int cnt,pre[N]; int que[N*1000]; int point[N],pedge[N]; int mxf; void add_edge(int u,int v,int w,int c) { edge[cnt].to=v; edge[cnt].w=w; edge[cnt].c=c; edge[cnt].next = pre[u]; pre[u]=cnt++; } int spfa() { int qd=0,qf=1; int dis[N]; int mark[N]; memset(point,-1,sizeof(point)); memset(pedge,-1,sizeof(pedge)); for(int i=0;i<=t;i++) { dis[i]=INF; mark[i]=0; } dis[s]=0; que[0]=s; mark[s]=1; while(qf>qd) { int cur=que[qd++]; mark[cur]=0; for(int p=pre[cur];p!=-1;p = edge[p].next) { int v=edge[p].to; int c=edge[p].c; int w=edge[p].w; if(w==0) continue; if( dis[v]>dis[cur]+c ) { dis[v]=dis[cur]+c; point[v]=cur; pedge[v]=p; if(mark[v]==0) { mark[v]=1; que[qf++]=v; } } } } if( dis[t] == INF ) return 0; else return 1; } int fuc() { int sum=0; int mi=INF; int tmp=t; while(tmp!=s) { int p=pedge[tmp]; if(edge[p].w<mi) mi=edge[p].w; tmp=point[tmp]; } tmp=t; mxf += mi; while(tmp!=s) { int p=pedge[tmp]; edge[p].w -= mi; edge[p^1].w += mi; sum += mi*edge[p].c; tmp=point[tmp]; } return sum; } int main() { int ans; while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k)) { int sum=0; mxf=0; ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=k;j++) { scanf("%d",&sn[i][j]); ans += sn[i][j]; } for(int i=1;i<=m;i++) for(int j=1;j<=k;j++) scanf("%d",&sm[i][j]); for(int i=1;i<=k;i++) { cnt=0; memset(pre,-1,sizeof(pre)); s=0; t=n+m+1; for(int j=1;j<=n;j++) { add_edge(s,j,sn[j][i],0); add_edge(j,s,0,0); } for(int j=1;j<=m;j++) { add_edge(n+j,t,sm[j][i],0); add_edge(t,n+j,0,0); } for(int j=1;j<=n;j++) for(int i1=1;i1<=m;i1++) { int tmp; scanf("%d",&tmp); add_edge(j,n+i1,INF,tmp); add_edge(n+i1,j,0,-tmp); } while(spfa()) { sum += fuc(); } } if( mxf != ans ) printf("-1\n"); else printf("%d\n",sum); } return 0; }