码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2516 Minimum Cost

时间:2014-09-13 21:20:15      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   java   ar   

Minimum Cost

Time Limit: 4000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2516
64-bit integer IO format: %lld      Java class name: Main
 
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

Source

 
解题:费用流。有k种商品,每次求运算某一种商品所需的最小费用。当然,要先判断,供应是不是能满足需求。
 
建图,创库到商店的边费用即为费用,流量为INF。商店到汇点的费用为0,流量为该商店的需求量。源点到创库的流量为该仓库此种商品的数量。费用为0.
 
输入很叽歪啊,耐心阅读。
 
bubuko.com,布布扣
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 200;
 18 struct arc {
 19     int v,w,f,next;
 20     arc(int x = 0,int y = 0,int z = 0,int nxt = 0) {
 21         v = x;
 22         w = y;
 23         f = z;
 24         next = nxt;
 25     }
 26 };
 27 arc e[10000];
 28 int head[maxn],d[maxn],p[maxn],tot,n,m,k,S,T;
 29 bool in[maxn];
 30 int need[maxn][maxn],supp[maxn][maxn];
 31 void add(int u,int v,int w,int f){
 32     e[tot] = arc(v,w,f,head[u]);
 33     head[u] = tot++;
 34     e[tot] = arc(u,-w,0,head[v]);
 35     head[v] = tot++;
 36 }
 37 bool spfa() {
 38     for(int i = S; i <= T; i++) {
 39         in[i] = false;
 40         d[i] = INF;
 41         p[i] = -1;
 42     }
 43     queue<int>q;
 44     d[S] = 0;
 45     in[S]  =true;
 46     q.push(S);
 47     while(!q.empty()) {
 48         int u = q.front();
 49         q.pop();
 50         in[u] = false;
 51         for(int i = head[u]; ~i; i = e[i].next) {
 52             if(e[i].f > 0 && d[e[i].v] > d[u] + e[i].w) {
 53                 d[e[i].v] = d[u] + e[i].w;
 54                 p[e[i].v] = i;
 55                 if(!in[e[i].v]) {
 56                     q.push(e[i].v);
 57                     in[e[i].v] = true;
 58                 }
 59             }
 60         }
 61     }
 62     return p[T] > -1;
 63 }
 64 int solve() {
 65     int tmp = 0,theMin;
 66     while(spfa()) {
 67         theMin = INF;
 68         for(int i = p[T]; ~i; i = p[e[i^1].v])
 69             theMin = min(theMin,e[i].f);
 70         for(int i = p[T]; ~i; i = p[e[i^1].v]) {
 71             e[i].f -= theMin;
 72             e[i^1].f += theMin;
 73             tmp += e[i].w*theMin;
 74         }
 75     }
 76     return tmp;
 77 }
 78 int main() {
 79     bool flag;
 80     int tmp,ans;
 81     while(scanf("%d %d %d",&n,&m,&k),n||m||k) {
 82         for(int i = 1; i <= n; i++)
 83             for(int j = 1; j <= k; j++)
 84                 scanf("%d",need[i]+j);
 85         for(int i = 1; i <= m; i++)
 86             for(int j = 1; j <= k; j++)
 87                 scanf("%d",supp[i]+j);
 88         flag = true;
 89         for(int i = 1; i <= k; i++){
 90             int sum = 0;
 91             for(int j = 1; j <= m; j++)
 92                 sum += supp[j][i];
 93             for(int j = 1; j <= n; j++)
 94                 sum -= need[j][i];
 95             if(sum < 0){flag = false;break;}
 96         }
 97         S = ans = 0;
 98         T = n+m+1;
 99         for(int i = 1; i <= k; i++){
100             memset(head,-1,sizeof(head));
101             tot = 0;
102             for(int j = 1; j <= m; j++)
103                 add(S,j,0,supp[j][i]);
104             for(int j = 1; j <= n; j++){
105                 add(m+j,T,0,need[j][i]);
106                 for(int t = 1; t <= m; t++){
107                     scanf("%d",&tmp);
108                     add(t,m+j,tmp,INF);
109                 }
110             }
111             if(flag) ans += solve();
112         }
113         printf("%d\n",flag?ans:-1);
114     }
115     return 0;
116 }
View Code

 

POJ 2516 Minimum Cost

标签:des   style   blog   http   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/crackpotisback/p/3970311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!