首页 > 代码库 > POJ训练计划2516_Minimum Cost(网络流/费用流)
POJ训练计划2516_Minimum Cost(网络流/费用流)
解题报告
题意:
有n个商店,m个提供商,k种商品</span> n*k的矩阵,表示每个商店需要每个商品的数目; m*k矩阵,表示每个提供商拥有每个商品的个数 然后对于每个物品k,都有n*m的矩阵 i行j列表示 从j提供商向i商店运送一个k商品的代价是多少 判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用
思路:
建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用,
不过这样其实不行的,会发现n跟k的关系没办法解决
如果把k种商品分k次处理就行了,相当于一次运一种
对于每一种商品,建一次图,求最小费用,如果最大流不能满足顾客的需求就pass。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; struct E { int v,cost,cap,next; } edge[100000]; int head[5000],dis[5000],pre[5000],vis[5000],f[5000],m1[55][55],m2[55][55],s,t,n,m,k,cnt,cost,flow; void add(int u,int v,int cost,int cap) { edge[cnt].v=v; edge[cnt].cost=cost; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cost=-cost; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++; } int _spfa() { for(int i=s; i<=t; i++) { dis[i]=inf,vis[i]=pre[i]=f[i]=0; } dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf; queue<int>Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) { pre[v]=i; dis[v]=dis[u]+edge[i].cost; f[v]=min(f[u],edge[i].cap); if(!vis[v]) { vis[v]=1; Q.push(v); } } } } if(dis[t]==inf)return 0; flow+=f[t]; cost+=f[t]*dis[t]; for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) { edge[i].cap-=f[t]; edge[i^1].cap+=f[t]; } return 1; } void mcmf() { cost=flow=0; while(_spfa()); } int main() { int i,j,l,a,b,c; while(~scanf("%d%d%d",&n,&m,&k)) { int sum[1000]; s=0,t=m+n+1; if(!n&&!m&&!k)break; memset(sum,0,sizeof(sum)); memset(m1,0,sizeof(m1)); memset(m2,0,sizeof(m2)); for(i=1; i<=n; i++) { for(j=1; j<=k; j++) { scanf("%d",&m1[i][j]); sum[j]+=m1[i][j]; } } for(i=1; i<=m; i++) { for(j=1; j<=k; j++) scanf("%d",&m2[i][j]); } int f=0; int ans=0; for(l=1; l<=k; l++) { cnt=0; memset(head,-1,sizeof(head)); memset(edge,0,sizeof(edge)); for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { scanf("%d",&a); add(j,m+i,a,inf); } } if(f)continue; for(i=1; i<=m; i++) add(s,i,0,m2[i][l]); for(i=1; i<=n; i++) add(m+i,t,0,m1[i][l]); mcmf(); if(flow<sum[l]) { f=1; } else ans+=cost; } if(f) printf("-1\n"); else printf("%d\n",ans); } }
Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 13529 | Accepted: 4633 |
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。