http://blog.csdn.net/lyy289065406/article/details/6742534
题意:
有N个供应商,M个店主,K种物品。每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商Mj送第kind种货物的单位数量到店主Ni手上所需的单位花费。
问:供应是否满足需求?如果满足,最小运费是多少?
分析:一开始将一个点拆成K个点,给过TLE了。。后来看题解发现分别把k种商品用最小费用最大流算出来就行了。。只要其中一种不满足,结果输出-1。
手打的最小费用最大流模版,结果打错了,调试了半天。。伤心啊。。。在增加边的时候加的两条都是from->to。。都是复制粘贴的错。
// File Name: 2516.cpp // Author: zlbing // Created Time: 2013/3/3 22:17:43 #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> using namespace std; #define CL(x,v); memset(x,v,sizeof(x)); #define INF 0x3f3f3f3f #define LL long long #define MAXN 105 #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<n+1;i++) struct Edge{ int from,to,cap,flow,cost; }; struct MCMF{ int n,m,s,t; vector<Edge>edges; vector<int> G[MAXN]; int inq[MAXN]; int d[MAXN]; int p[MAXN]; int a[MAXN]; void init(int n){ this->n=n; for(int i=0;i<=n;i++)G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap,int cost){ edges.push_back((Edge){from,to,cap,0,cost}); edges.push_back((Edge){to,from,0,0,-cost}); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BellmanFord(int s,int t,int& flow,int& cost){ for(int i=0;i<=n;i++)d[i]=INF; CL(inq,0); d[s]=0;inq[s]=1;p[s]=0;a[s]=INF; queue<int>Q; Q.push(s); while(!Q.empty()){ int u=Q.front();Q.pop(); inq[u]=0; for(int i=0;i<G[u].size();i++){ Edge& e=edges[G[u][i]]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){ d[e.to]=d[u]+e.cost; p[e.to]=G[u][i]; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]){ Q.push(e.to); inq[e.to]=1; } } } } if(d[t]==INF)return false; flow+=a[t]; cost+=d[t]*a[t]; int u=t; while(u!=s){ edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; u=edges[p[u]].from; } return true; } int Mincost(int s,int t,int sum){ int flow=0,cost=0; while(BellmanFord(s,t,flow,cost)); if(flow!=sum)return -1; return cost; } }; MCMF solver; int shop[MAXN][MAXN]; int supply[MAXN][MAXN]; int sum[MAXN]; int main(){ int n,m,k; while(~scanf("%d%d%d",&n,&m,&k)) { if(!n)break; int N=n+m+1; CL(sum,0); REP1(i,n){ int a; REP1(j,k){ scanf("%d",&a); sum[j]+=a; shop[i][j]=a; } } REP1(i,m){ int a; REP1(j,k){ scanf("%d",&a); supply[i][j]=a; } } int ans=0; bool flag=true; REP1(t,k){ int a; solver.init(N); REP1(x,n) solver.AddEdge(x,N,shop[x][t],0); REP1(x,m) solver.AddEdge(0,x+n,supply[x][t],0); REP1(i,n){ REP1(j,m){ scanf("%d",&a); solver.AddEdge(j+n,i,INF,a); } } int tmp=solver.Mincost(0,N,sum[t]); if(tmp==-1)flag=false; ans+=tmp; } if(!flag)printf("-1\n"); else printf("%d\n",ans); } return 0; }