poj 2516 Minimum Cost(费用流)
2018-01-11 21:24
411 查看
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
Sample Output
花费的话只有供应商和店之间有花费
可以在之前提前判断一下每个货物的总量是否满足,优化一些时间
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 17714 | Accepted: 6225 |
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
题意:给出n个店,m个供应商,k种货,然后是n行,每行是店对每种货物的需求量,接着有k个n*m的矩阵,表示j对i运一件货的花费是多少,求所有店都可以满足需求的最小花费 ,如果存在店不满足需求,输出-1
费用流问题,将原图拆成k个图,每张图如下所示(只写了流量)花费的话只有供应商和店之间有花费
可以在之前提前判断一下每个货物的总量是否满足,优化一些时间
#include <cstdio> #include <cstring> #include <cctype> #include <stdlib.h> #include <string> #include <map> #include <iostream> #include <stack> #include <cmath> #include <queue> #include <vector> #include <algorithm> using namespace std; typedef long long ll; #define inf 0x3f3f3f3f #define mem(a,b) memset(a,b,sizeof(a)) const int N=5000+20; const int M=50000+20; int top;//当前边下标 int dis ,pre ;//源点到点i的最小距离,pre[i]记录前驱 bool vis ;//标记数组 int maxflow; struct vertex { int first; } V ; struct Edge dc80 { int v,next; int cap,flow,cost; } E[M*2]; void init() { mem(V,-1); top=0; maxflow=0; } void add_edge(int u,int v,int c,int cost) { E[top].v=v; E[top].cap=c; E[top].flow=0; E[top].cost=cost; E[top].next=V[u].first; V[u].first=top++; } void add(int u,int v,int c,int cost) { add_edge(u,v,c,cost); add_edge(v,u,0,-cost); } bool spfa(int s,int t) { int i,u,v; queue<int>q; mem(vis,false); mem(pre,-1); mem(dis,inf); vis[s]=true; dis[s]=0; q.push(s); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=false; for(int i=V[u].first; i!=-1; i=E[i].next) { v=E[i].v; if(E[i].cap>E[i].flow&&dis[v]>dis[u]+E[i].cost) { dis[v]=dis[u]+E[i].cost; pre[v]=i; if(!vis[v]) { q.push(v); vis[v]=true; } } } } if(dis[t]==inf) return false; return true; } int MCMF(int s,int t)//minCostMaxFlow { int d; int i,mincost=0;//maxflow当前最大流量,mincost当前最小费用 while(spfa(s,t))//表示找到了从s到t的最小费用路 { d=inf; for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //遍历反向边 d=min(d,E[i].cap-E[i].flow); maxflow+=d;//更新最大流 //printf("%d\n",d); for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //增广路上正向边流量+d,反向边流量-d { E[i].flow+=d; E[i^1].flow-=d; } mincost+=dis[t]*d;//dis[t]为该路径上单位流量费用之和 } return mincost; } int e1[55][55],e2[55][55],w[55][55],check[55]; int main() { int n,m,k,i,j,o; while(scanf("%d%d%d",&n,&m,&k)&&n+m+k) { int ans=0; mem(check,0); for(i=1; i<=n; i++) for(j=1; j<=k; j++) { scanf("%d",&e1[i][j]); check[j]+=e1[i][j]; } for(i=1; i<=m; i++) for(j=1; j<=k; j++) { scanf("%d",&e2[i][j]); } bool flag=0; for(o=1; o<=k; o++) { //源-》供货 init(); for(i=1; i<=n; i++) { //供货-》店 for(j=1; j<=m; j++) { scanf("%d",&w[j][i]); add(j,m+i,e2[j][o],w[j][i]); } } if(flag==1)continue; for(i=1;i<=m;i++) add(0,i,e2[i][o],0); for(i=1;i<=n;i++) add(m+i,n+m+1,e1[i][o],0); int now=MCMF(0,m+n+1); if(maxflow!=check[o]) { flag=1; continue; } else ans+=now; } if(flag) puts("-1"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 2516 - Minimum Cost 分为几次做最小费用最大流...
- 【网络流】【费用流】[POJ 2516]Minimum Cost
- Poj 2516 Minimum Cost【费用流Min_Cost_Max_flow】
- POJ 2516 Minimum Cost 费用流
- poj 2516 Minimum Cost 【最小费用最大流】【求解K种物品的最小费用,独立求解累加每个结果】
- POJ 2516 — Minimum Cost 费用流
- POJ 2516 Minimum Cost 费用流
- 文章标题 POJ 2516 : Minimum Cost (求k次 费用流)
- POJ 2516 Minimum Cost(费用流)
- POJ训练计划2516_Minimum Cost(网络流/费用流)
- POJ 2516 Minimum Cost 最小费用最大流 费用流
- [POJ 2516]Minimum Cost[费用流][建图]
- 【POJ】 2516 Minimum Cost 费用流
- poj 2516 Minimum Cost (费用流)
- POJ 2516 Minimum Cost(网络流之费用流)
- POJ 2516 Minimum Cost (费用流)
- POJ 2516 Minimum Cost 最大流的最小费用(不要拆点!)
- POJ 2516 Minimum Cost(费用流)
- E - Minimum Cost - POJ 2516(最小费)
- Minimum Cost POJ - 2516 费用流