POJ 2516 Minimum Cost (最小费用最大流)
2017-09-05 18:36
429 查看
Minimum Cost
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 intege 4000 rs 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 |
N个店,M个供应商,K种商品。
给你每个店的K个商品的需求,供应商的K个商品的存货,每种商品的(第j个供应商给第i个店)的价格【一个n*m矩阵】。
POINT:
因为商品太多,我们一个商品一个商品做。
s-供应商。cost为0,cap为商品数。
供应商-店。cost为给你的值,cap为inf。
店-t。cost为0,cap为需求。
然后最小费用最大流做。
另外输出-1,比较一下。(不要让程序提前结束,无谓的wa)
#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <math.h>
using namespace std;
#define LL long long
typedef pair<int,int> pr;
const int maxn =222;
const int inf = 0x3f3f3f3f;
int s,t;
struct edge
{
int from,to,cap,flow,cost;
edge(int u,int v,int c,int f,int dd):
from(u),to(v),cap(c),flow(f),cost(dd){}
};
vector<int>G[maxn];
vector<edge>len;
int n,m,k;
int need[maxn][maxn];
int needk[maxn];
int have[maxn][maxn];
int havek[maxn];
void add(int u,int v,int c,int cost)
{
len.push_back(edge(u,v,c,0,cost));
len.push_back(edge(v,u,0,0,-cost));
G[u].push_back(len.size()-2);
G[v].push_back(len.size()-1);
}
void init()
{
for(int i=0;i<maxn;i++) G[i].clear();
len.clear();
}
int dis[maxn],inq[maxn],pre[maxn];
bool spfa(int &ans)
{
queue<int>q;
for(int i=0;i<=t;i++) dis[i]=inf,pre[i]=0;
dis[s]=0;
inq[s]=1,q.push(s);
int a=inf;
while(!q.empty())
{
int u=q.front();
q.pop(),inq[u]=0;
for(int i=0;i<G[u].size();i++)
{
edge e = len[G[u][i]];
if(e.cap>e.flow&&dis[e.to]>dis[u]+e.cost)
{
dis[e.to]=dis[u]+e.cost;
pre[e.to]=G[u][i];
a=min(a,e.cap-e.flow);
if(!inq[e.to])
{
inq[e.to]=1;
q.push(e.to);
}
}
}
}
if(dis[t]==inf) return 0;
int u=t;
ans+=dis[t]*a;
while(u!=s)
{
len[pre[u]].flow+=a;
len[pre[u]^1].flow-=a;
u=len[pre[u]].from;
}
return 1;
}
int mincost()
{
int ans=0;
while(spfa(ans))
{
continue;
}
return ans;
}
int main()
{
while(~scanf("%d %d %d",&n,&m,&k))
{
if(n==0&&n==m&&m==k) return 0;
memset(need,0,sizeof need);
memset(have,0,sizeof have);
memset(havek,0,sizeof havek);
memset(needk,0,sizeof needk);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
scanf("%d",&need[i][j]);
needk[j]+=need[i][j];
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=k;j++)
{
scanf("%d",&have[i][j]);
havek[j]+=have[i][j];
}
}
int flag=1;
for(int i=1;i<=k;i++)
{
if(needk[i]>havek[i])
{
printf("-1\n");
flag=0;
break;
}
}
int ans=0;
int cost[maxn][maxn];
s=0,t=n+m+1;
for(int kk=1;kk<=k;kk++)
{
memset(cost,0,sizeof cost);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&cost[i][j]);
}
}
if(!flag) continue;
init();//0 源点 1-m 供应 m+1-m+n 店 m+n+1 汇点
//add(int u,int v,int c,int cost)
for(int j=1;j<=m;j++)
{
add(0,j,have[j][kk],0);
for(int i=1;i<=n;i++)
{
add(j,m+i,inf,cost[i][j]);
}
}
for(int i=1;i<=n;i++)
{
add(m+i,t,need[i][kk],0);
}
ans+=mincost();
}
if(!flag) continue;
printf("%d\n",ans);
}
}
相关文章推荐
- POJ 2516 Minimum Cost (最小费用最大流)
- 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--最小费用最大流
- (最小费用最大流)POJ2516 Minimum Cost
- POJ 2516 Minimum Cost(最小费用最大流-mcmf)
- poj 2516 Minimum Cost 最小费用最大流
- POJ - 2516 Minimum Cost (最小费用最大流)
- poj 2516 Minimum Cost(最小费用最大流 spfa算法求最短路)
- POJ 2516 Minimum Cost(最小费用最大流)
- POJ 2516 Minimum Cost (最小费用最大流)
- poj-2516-Minimum Cost-最小费用最大流
- POJ 2516 Minimum Cost(最小费用最大流)
- poj 2516 Minimum Cost(最小费用最大流)