# POJ 2516 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".

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

4
-1

## 代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int maxN=300;
const int inf=2147483647;

int n,m,K;
int Path[maxN];//跑最大流时记录下某节点的前驱
int Flow[maxN];//记录到每一个点时的流量
int Dist[maxN];//spfa以话费跑最小值的到每一个点的距离
int ques[maxN][maxN];//保存每一家商店的需求
int prov[maxN][maxN];//保存每一家供应商的存货
int G[maxN][maxN];//流量图
int C[maxN][maxN];//费用图
bool inqueue[maxN];//是否在队列中，spfa用

bool spfa();

int main()
{
while (cin>>n>>m>>K)//多组数据
{
if ((n==0)&&(m==0)&&(K==0))
break;
for (int i=1;i<=n;i++)//输入
for (int j=1;j<=K;j++)
scanf("%d",&ques[i][j]);
for (int i=1;i<=m;i++)
for (int j=1;j<=K;j++)
scanf("%d",&prov[i][j]);
int Ans=0;
bool had_ans=1;//标记是否有解
for (int i=1;i<=K;i++)//判断是否有解，即供应量是否大于需求量
{
int sum1=0;
for (int j=1;j<=n;j++)
sum1+=ques[j][i];
int sum2=0;
for (int j=1;j<=m;j++)
sum2+=prov[j][i];
if (sum2<sum1)
{
had_ans=0;
break;
}
}
for (int ki=1;ki<=K;ki++)//注意，即使我们前面判断出了无解，也要把数据读完，因为有多组数据
{
memset(G,0,sizeof(G));//每次都要清空
memset(C,0,sizeof(C));
for (int i=1;i<=n;i++)//读入费用
for (int j=1;j<=m;j++)
{
scanf("%d",&C[i][j+n]);//建正向边
C[j+n][i]=-C[i][j+n];//建反向边
G[i][j+n]=inf;//连接相应的商店和供应商，这里[1,n]是商店，[n+1,n+m]是供应商
}
if (had_ans==0)//如果是无解情况，直接进入下一层循环把输入读完即可
continue;
for (int i=1;i<=n;i++)//连源点到商店，这里源点是0
G[0][i]=ques[i][ki];
for (int i=1;i<=m;i++)//连供应商到汇点，汇点是n+m+1
G[i+n][n+m+1]=prov[i][ki];
int cost=0;//记录当前第K中商品的总花费
while (spfa())//spfa求出花费最小的增广路
{
int now=n+m+1;
int last=Path[now];
cost+=Flow[n+m+1]*Dist[n+m+1];//先累计一下花费
while (now!=0)//从汇点出发，依次修改路径上的残量网络
{
G[last][now]-=Flow[n+m+1];
G[now][last]+=Flow[n+m+1];
now=last;
last=Path[now];
}
}
Ans+=cost;//最后总答案累加每一个K种货物的总和
}
if (had_ans)//若有解则输出解，否则输出-1
cout<<Ans<<endl;
else
cout<<-1<<endl;
}
return 0;
}

bool spfa()//spfa求花费最小的增广路
{
memset(Dist,127,sizeof(Dist));//初始化
memset(Path,-1,sizeof(Path));
memset(Flow,0,sizeof(Flow));
memset(inqueue,0,sizeof(inqueue));
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);//把源点放入队列
Dist[0]=0;
Flow[0]=inf;
do
{
int u=Q.front();
inqueue[u]=0;
Q.pop();
for (int i=0;i<=n+m+1;i++)
if ((G[u][i]>0)&&(Dist[i]>Dist[u]+C[u][i]))//这里要求有流量且i原来的花费大于u的花费+路径上的花费
{
Dist[i]=Dist[u]+C[u][i];//用u的数据修改i的
Path[i]=u;
Flow[i]=min(Flow[u],G[u][i]);
if (inqueue[i]==0)
{
Q.push(i);
inqueue[i]=1;
}
}
}
while (!Q.empty());
if (Path[n+m+1]==-1)//如果汇点没有经过过，则说明增广完毕，不存在增广路了
return 0;
return 1;
}

posted @ 2017-07-30 16:07  SYCstudio  阅读(478)  评论(0编辑  收藏