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 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
POJ Monthly--2005.07.31, Wang Yijie
题目类型:最小费用最大流
算法分析:首先将每个物品需求量和供给量读入并判断,若供不应求则输出-1。否则对于每个物品ki,跑一个最小费用流,建图方式为:从源点s向每个供应商连一条容量为当前供应商ki供应量,花费为0的有向边,从每个店主向汇点e连一条容量为当前店主ki需求量,花费为0的有向边。对于每个给出的从供应商到店主的关系,连一条容量为INF,花费为所给花费的有向边。最后将每次费用流得到的花费累加即可。注意不拆开建图会TLE!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
/************************************************* Author :supermaker Created Time :2016/4/4 21:18:22 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5000 + 66; struct Node { int to, nxt, c, f, cost; }; Node edge[maxn*66]; int head[maxn], edgelen; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int cost, int u, int v, int w, int rw = 0) { edge[edgelen].to = v, edge[edgelen].c = w, edge[edgelen].f = 0; edge[edgelen].cost = cost, edge[edgelen].nxt = head[u], head[u] = edgelen++; edge[edgelen].to = u, edge[edgelen].c = rw, edge[edgelen].f = 0; edge[edgelen].cost = -cost, edge[edgelen].nxt = head[v], head[v] = edgelen++; } int dis[maxn], cnt[maxn], inq[maxn], par[maxn]; int N; bool spfa (int s, int e) { for (int i = 0; i <= N; i++) { dis[i] = INF; inq[i] = cnt[i] = 0; par[i] = -1; } dis[s] = 0; queue <int> qu; qu.push (s); inq[s]++, cnt[s]++; while (!qu.empty ()) { int u = qu.front (); qu.pop (); inq[u]--; if (cnt[u] > N) return false; for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (edge[i].c - edge[i].f && dis[u] < INF && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; par[v] = i; if (!inq[v]) { qu.push (v); inq[v]++, cnt[v]++; } } } } if (par[e] == -1) return false; return true; } int allcost; int MinCostMaxFlow (int s, int e) { int res = 0; allcost = 0; while (spfa (s, e)) { int minval = INF; for (int i = par[e]; i != -1; i = par[edge[i^1].to]) minval = min (minval, edge[i].c - edge[i].f); for (int i = par[e]; i != -1; i = par[edge[i^1].to]) { edge[i].f += minval; edge[i^1].f -= minval; allcost += edge[i].cost * minval; } res += minval; } return res; } int aa[66][66], bb[66][66]; int numsup[66], numneed[66]; int main() { //CFF; //CPPFF; int num, M, K; while (scanf ("%d%d%d", &num, &M, &K) != EOF) { if (num == 0 && M == 0 && K == 0) break; memset (numsup, 0, sizeof (numsup)); memset (numneed, 0, sizeof (numneed)); for (int i = 1; i <= num; i++) for (int j = 1; j <= K; j++) { scanf ("%d", &aa[i][j]); numneed[j] += aa[i][j]; } for (int i = 1; i <= M; i++) for (int j = 1; j <= K; j++) { scanf ("%d", &bb[i][j]); numsup[j] += bb[i][j]; } bool is_valid = true; for (int i = 1; i <= K; i++) if (numneed[i] > numsup[i]) { is_valid = false; break; } int ans = 0; for (int k = 1; k <= K; k++) { Init (); int val; for (int i = 1; i <= num; i++) for (int j = 1; j <= M; j++) { scanf ("%d", &val); AddEdge (val, num + j, i, INF); } N = num + M + 2; for (int i = 1; i <= M; i++) AddEdge (0, 0, num + i, bb[i][k]); for (int i = 1; i <= num; i++) AddEdge (0, i, N - 1, aa[i][k]); MinCostMaxFlow (0, N - 1); ans += allcost; } if (!is_valid) puts ("-1"); else printf ("%d\n", ans); } return 0; } |
- « 上一篇:poj2195
- poj2175:下一篇 »