您的位置:首页 > 其它

poj 2516 Minimum Cost 最小费用流

2015-11-28 22:42 239 查看
Minimum Cost

Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 15183Accepted: 5228
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

//题意有点麻烦,就不说了
//刚开始做的时候,一次建图,结果各种TLE,实在撸不出来,然后百度,匆匆扫了两眼,说是不要一次建图,对每个商品建图,然后滚回改,改了半天各种WA,我也是醉了
//若供货点的某一种货物数量小于商店的需要,则无解输出-1
//建图时,用超级源点0连接所有的供货点,超级汇点n+m+1连接所有的商店,源点和供货点之间容量为供货点的当前货物数量,费用为0,商店和源点之间的容量是商店需要的当前货物数量,费用为0。至于供货点和商店之间的边,容量可以是供货点的当前货物数量,也可以是商店需要的数量,都可以,费用即为题中给的费用,然后最小费用流模板
//总结:建图是个痛苦的过程,想了N久,还跪了。其实是个水题,被几个数组搞晕了,。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int N = 70;
const int INF = 0x3f3f3f3f;
int n, m, k;

struct edge
{
int to, cap, cost, rev;
};
vector <edge> G[N*5];
int dist[N*5], prevv[N*5], preve[N*5];
bool used[N*5];

void add_edge(int from, int to, int cap, int cost)
{
edge e;
e.to = to, e.cap = cap, e.cost = cost, e.rev = G[to].size();
G[from].push_back(e);
e.to = from, e.cap = 0, e.cost = -cost, e.rev = G[from].size() - 1;
G[to].push_back(e);
}

int min_cost_flow(int s, int t, int f)
{
int res = 0;
while(f > 0)
{
memset(dist, 0x3f, sizeof dist);
memset(used, 0, sizeof used);
queue <int> que;
dist[s] = 0;
used[s] = true;
que.push(s);

while(! que.empty())
{
int v = que.front(); que.pop();
for(int i = 0; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost)
{
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
if(! used[e.to])
{
que.push(e.to);
used[e.to] = true;
}
}
}
used[v] = false;

}
if(dist[t] == INF) return -1;

int d = f;
for(int i = t; i != s; i = prevv[i])
d = min(d, G[prevv[i]][preve[i]].cap);

f -= d;
res += d * dist[t];
for(int i = t; i != s; i = prevv[i])
{
edge &e = G[prevv[i]][preve[i]];
e.cap -= d;
G[i][e.rev].cap += d;
}
}

return res;
}

int main()
{
int shoper

, supply

;
int mpa

;
int need1
, need2
;

while(scanf("%d%d%d", &n, &m, &k), n != 0 || m != 0 || k != 0)
{
memset(need1, 0, sizeof need1);
memset(need2, 0, sizeof need2);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= k; j++)
{
scanf("%d", &shoper[i][j]);
need1[j] += shoper[i][j];
}

for(int i = 1; i <= m; i++)
for(int j = 1; j <= k; j++)
{
scanf("%d", &supply[i][j]);
need2[j] += supply[i][j];
}

int f = 1;
for(int i = 1; i <= n; i++)
if(need1[i] > need2[i])
f = 0;

for(int i = 1; i <= k; i++)
for(int j = 1; j <= n; j++)
for(int l = 1; l <= m; l++)
scanf("%d", &mpa[i][j][l]);

if(f == 0) printf("-1\n");
else
{
int res = 0, tmp;
for(int l = 1; l <= k; l++)
{
for(int i = 1; i <= m; i++)
add_edge(0, i, supply[i][l], 0);
for(int i = m + 1; i <= m + n; i++)
add_edge(i, m + n + 1, shoper[i-m][l], 0);

for(int i = 1; i <= m; i++)
for(int j = m + 1; j <= m + n; j++)
add_edge(i, j, shoper[j-m][l], mpa[l][j-m][i]);

res += min_cost_flow(0, m + n + 1, need1[l]);
for(int i = 0; i <= m + n + 1; i++)
G[i].clear();
}
printf("%d\n", res);
}
}

return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: