ACM Computer Factory
As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.
Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.
Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.
Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.
Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.
The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.
After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.
As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.
Input
Input file contains integers P N, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2...Si,P Di,1 Di,2...Di,P, where Qi specifies performance, Si,j — input specification for part j, Di,k — output specification for part k.
Constraints
1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 10000
Output
Output the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines A andB must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.
If several solutions exist, output any of them.
Sample Input
Sample input 13 4
15 0 0 0 0 1 0
10 0 0 0 0 1 1
30 0 1 2 1 1 1
3 0 2 1 1 1 1
Sample input2
3 5
5 0 0 0 0 1 0
100 0 1 0 1 0 1
3 0 1 0 1 1 0
1 1 0 1 1 1 0
300 1 1 2 1 1 1
Sample input 3
2 2
100 0 0 1 0
200 0 1 1 1
Sample Output
Sample output 1
25 2
1 3 15
2 3 10
Sample output 2
4 5
1 3 3
3 5 3
12 1
2 4 1
4 5 1
Sample output 3
0 0
Hint
Bold texts appearing in the sample sections are informative and do not form part of the actual data.
Source
Northeastern Europe 2005, Far-Eastern Subregion
题目类型:最大流
算法分析:将输入规格和输出规格能够匹配的两个机器之间连一条有向边(从输出规格所在的点到输入规格所在的点),容量为输出规格的机器性能。从源点向输入规格为非1的机器之间连一条有向边,容量为当前机器的性能。每个输出规格为非0的机器向汇点连一条容量为当前机器性能的有向边,最后跑一个最大流即可。在修改边流量的同时将被修改的原图中的边记录下来,这里在邻接表中额外增加变量id表示有向边的起始节点。注意源点、汇点在和其它点相连时的条件!!!
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
/************************************************* Author :supermaker Created Time :2016/3/27 10:27:50 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 = 200 + 66; struct Node { int to, nxt, c, f; int id; }; Node edge[maxn*maxn+3*maxn]; int head[maxn], edgelen; int dep[maxn], cnt[maxn], cur[maxn]; int ans[maxn][6], len; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int w, int rw = 0) { edge[edgelen].to = v, edge[edgelen].c = w, edge[edgelen].f = 0, edge[edgelen].id = u; edge[edgelen].nxt = head[u], head[u] = edgelen++; edge[edgelen].to = u, edge[edgelen].c = rw, edge[edgelen].f = 0, edge[edgelen].id = v; edge[edgelen].nxt = head[v], head[v] = edgelen++; } void bfs (int s, int e) { memset (dep, -1, sizeof (dep)); memset (cnt, 0, sizeof (cnt)); dep[e] = 0; cnt[dep[e]]++; queue <int> qu; qu.push (e); while (!qu.empty ()) { int u = qu.front (); qu.pop (); for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (dep[v] != -1) continue; qu.push (v); dep[v] = dep[u] + 1; cnt[dep[v]]++; } } } int in[maxn][maxn], out[maxn][maxn], cap[maxn]; int P, N; int sta[maxn]; int MaxFlow (int s, int e, int n) { bfs (s, e); int res = 0, top = 0, u = s; memcpy (cur, head, sizeof (head)); while (dep[s] < n) { if (u == e) { int minval = INF, minval_pos = -1; for (int i = 0; i < top; i++) if (minval > edge[sta[i]].c - edge[sta[i]].f) { minval = edge[sta[i]].c - edge[sta[i]].f; minval_pos = i; } for (int i = 0; i < top; i++) { edge[sta[i]].f += minval; edge[sta[i]^1].f -= minval; if (edge[sta[i]].id >= 1 && edge[sta[i]].id <= N && edge[sta[i]].to >= 1 && edge[sta[i]].to <= N) ans[len][1] = edge[sta[i]].id, ans[len][2] = edge[sta[i]].to, ans[len++][3] = minval; } res += minval; top = minval_pos; u = edge[sta[top]^1].to; } else { bool is_have = false; for (int i = cur[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (edge[i].c - edge[i].f && dep[v] + 1 == dep[u]) { cur[u] = i; sta[top++] = cur[u]; u = v; is_have = true; break; } } if (is_have) continue; int minval = n; for (int i= head[u]; i != -1; i = edge[i].nxt) if (edge[i].c - edge[i].f && dep[edge[i].to] < minval) { minval = dep[edge[i].to]; cur[u] = i; } cnt[dep[u]]--; if (!cnt[dep[u]]) return res; dep[u] = minval + 1; cnt[dep[u]]++; if (u != s) u = edge[sta[--top]^1].to; } } return res; } int main() { //CFF; //CPPFF; while (scanf ("%d%d", &P, &N) != EOF) { for (int i = 1; i <= N; i++) { scanf ("%d", &cap[i]); for (int j = 1; j <= P; j++) scanf ("%d", &in[i][j]); for (int j = 1; j <= P; j++) scanf ("%d", &out[i][j]); } Init (); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) if (i != j) { bool is_valid = true; for (int k = 1; k <= P; k++) { if (out[i][k] == 2 || in[j][k] == 2 || out[i][k] == in[j][k]) continue; is_valid = false; break; } if (is_valid) AddEdge (i, j, cap[i]); } for (int i = 1; i <= N; i++) { bool is_zero = true; for (int j = 1; j <= P; j++) if (in[i][j] == 1) { is_zero = false; break; } if (is_zero) AddEdge (0, i, cap[i]); } for (int i = 1; i <= N; i++) { bool is_one = true; for (int j = 1; j <= P; j++) if (out[i][j] == 0) { is_one = false; break; } if (is_one) AddEdge (i, N + 1, cap[i]); } len = 0; int res = MaxFlow (0, N + 1, N + 2); printf ("%d %d\n", res, len); for (int i = 0; i < len; i++) printf ("%d %d %d\n", ans[i][1], ans[i][2], ans[i][3]); } return 0; } |
- « 上一篇:poj2391
- poj3308:下一篇 »