您的位置:首页 > 其它

POJ 2516 Minimum Cost (最小费用最大流)

2013-05-08 18:22 330 查看
Minimum Cost

Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 11859Accepted: 3993
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
题目输入很烦,需要耐心。只要能想到把每件物品分开来处理就好办了,构图时源点连供应商,流量为供给量,费用为0;店主与汇点连边,流量为需求量,费用为0;再根据每个供应商供给给不同店主的运输费用各自连边,流量为无穷,费用为输入给出的费用。题目可能算是给了点暗示吧,因为对于每件物品都有一个N*M的矩阵表示运输费用,注意在N*M矩阵中,i 行 j 列表示的是第 j 个供应商给第 i 和店主供货时的运输费用,这就是考验细心的了。题目还要求如果不能满足需求,则输出-1。所以提前统计一下每种物品的总需求量和总供给量,如果某物品的 需求量>供给量 则标记一下,结果输出-1.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define SIZE 3000
#define inf 0xfffffff

using namespace std;

struct node
{
int to,val,cost,next;
}edge[SIZE*668];

int N,M,K,sc,sk,pt,sum;
int head[SIZE],idx;
int offer[64][64],need[64][64],fare[64][64][64];
int demand[64],supply[64];
int dis[SIZE],pre[SIZE],pos[SIZE];
bool vis[SIZE];

void addnode(int from,int to,int val,int cost)
{
edge[idx].to = to;
edge[idx].val = val;
edge[idx].cost = cost;
edge[idx].next = head[from];
head[from] = idx++;
edge[idx].to = from;
edge[idx].val = 0;
edge[idx].cost = -cost;
edge[idx].next = head[to];
head[to] = idx++;
}

bool spfa()
{
queue <int> Q;
for(int i=0; i<=pt; i++)
{
dis[i] = inf;
vis[i] = false;
}
dis[sc] = 0;
pre[sc] = sc;
vis[sc] = true;
Q.push(sc);
while(!Q.empty())
{
int cur = Q.front();
Q.pop();
vis[cur] = false;
for(int i=head[cur]; i!=-1; i=edge[i].next)
{
int to = edge[i].to;
if(edge[i].val > 0 && dis[to] > dis[cur] + edge[i].cost)
{
dis[to] = dis[cur] + edge[i].cost;
pre[to] = cur;
pos[to] = i;
if(!vis[to])
{
vis[to] = true;
Q.push(to);
}
}
}
}
if(pre[sk] != -1 && dis[sk] < inf)
return true;
return false;
}

int Flow()
{
int flow = 0,cost = 0;
while(spfa())
{
int Min = inf;
for(int i=sk; i!=sc; i=pre[i])
Min = min(Min,edge[pos[i]].val);
flow += Min;
cost += Min*dis[sk];
for(int i=sk; i!=sc; i=pre[i])
{
edge[pos[i]].val -= Min;
edge[pos[i]^1].val += Min;
}
}
return cost;
}

void read()
{
memset(demand,0,sizeof(demand));
memset(supply,0,sizeof(supply));
memset(need,0,sizeof(need));
memset(offer,0,sizeof(offer));
memset(fare,0,sizeof(fare));
for(int i=1; i<=N; i++)
{
for(int j=1; j<=K; j++)
{
scanf("%d",&need[i][j]);
demand[j] += need[i][j];
}
}
for(int i=1; i<=M; i++)
{
for(int j=1; j<=K; j++)
{
scanf("%d",&offer[i][j]);
supply[j] += offer[i][j];
}
}
for(int k=1; k<=K; k++)
for(int i=1; i<=N; i++)
for(int j=1; j<=M; j++)
scanf("%d",&fare[j][i][k]);
bool flag = false;
for(int i=1; i<=K; i++)
{
if(demand[i] > supply[i])
{
flag = true;
break;
}
}
if(flag)
{
puts("-1");
return;
}
int ans = 0;
for(int k=1; k<=K; k++)
{
idx = 0;
memset(head,-1,sizeof(head));
sc = 0, sk = N+M+1, pt = sk+1;
for(int i=1; i<=M; i++)
{
addnode(sc,i,offer[i][k],0);
for(int j=1; j<=N; j++)
addnode(i,M+j,inf,fare[i][j][k]);
}
for(int i=1; i<=N; i++)
addnode(M+i,sk,need[i][k],0);
ans += Flow();
}
printf("%d\n",ans);
}

int main()
{
while(~scanf("%d%d%d",&N,&M,&K))
{
if(!N && !M && !K)
break;
read();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: