ACM Computer Factory
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 10940 | Accepted: 4098 | Special Judge |
Description
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 and B 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 1 3 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 input 2 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 1 2 1 2 4 1 4 5 1 Sample output 3 0 0
Hint
Source
一发AC。。。都在题解里了。
1 /* 2 本题还是比较入门级别的水题吧,思路很好想,选出所有的源点和所有的汇点,还有所有的可行边(如果一台电脑能够接受上一台电脑输出之后的结果,就说明可以从他们之间建立一条边), 3 接着按照多源多汇和结点容量最大流建边,跑一波最大流就ok啦,还有就是记录路径和结点容量值,嘤嘤嘤。 4 如果需要打印路径和统计流量,就将图备份一次,并且在存图的时候存入边的起点就ok了,跑完最大流检查一遍哪些边被用过了就ok。 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 12 const int maxn = 50 + 5, maxm = 50 * 49 + 5, maxp = 10 + 5, inf = 0x3f3f3f3f; 13 int p, n, ps[maxn << 1][11], pe[maxn << 1][11], q[maxn << 1], s1[maxn << 1], e1[maxn << 1]; 14 int sizes, sizet, tot, tot1, cnt, head[maxn << 1], que[maxn << 1], dep[maxn << 1], cur[maxn << 1], sta[maxn << 1]; 15 struct Edge { 16 int to, next, cap, flow, from; 17 } edge[maxm << 1], edges[maxm << 1]; 18 struct node { 19 int u, v, w; 20 } ans[maxm << 1]; 21 22 void init() { 23 memset(head, -1, sizeof head); 24 tot = tot1 = 2; 25 sizes = sizet = cnt = 0; 26 } 27 28 void addedge(int u, int v, int w, int rw = 0) { 29 edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0; edge[tot].from = u; 30 edge[tot].next = head[u]; head[u] = tot ++; 31 edge[tot].to = u; edge[tot].cap = rw; edge[tot].flow = 0; edge[tot].from = v; 32 edge[tot].next = head[v]; head[v] = tot ++; 33 34 edges[tot1].to = v; edges[tot1].cap = w; edges[tot1].flow = 0; edges[tot1].from = u; 35 edges[tot1].next = head[u]; head[u] = tot1 ++; 36 edges[tot1].to = u; edges[tot1].cap = rw; edges[tot1].flow = 0; edges[tot1].from = v; 37 edges[tot1].next = head[v]; head[v] = tot1 ++; 38 } 39 40 bool bfs(int s, int t, int n) { 41 int front = 0, tail = 0; 42 memset(dep, -1, sizeof dep[0] * (n + 1)); 43 dep[s] = 0; 44 que[tail ++] = s; 45 while(front < tail) { 46 int u = que[front ++]; 47 for(int i = head[u]; ~i; i = edge[i].next) { 48 int v = edge[i].to; 49 if(edge[i].cap > edge[i].flow && dep[v] == -1) { 50 dep[v] = dep[u] + 1; 51 if(v == t) return true; 52 que[tail ++] = v; 53 } 54 } 55 } 56 return false; 57 } 58 59 int dinic(int s,int t, int n) { 60 int maxflow = 0; 61 while(bfs(s, t, n)) { 62 for(int i = 0; i < n; i ++) cur[i] = head[i]; 63 int u = s, tail = 0; 64 while(cur[s] != -1) { 65 if(u == t) { 66 int tp = inf; 67 for(int i = tail - 1; i >= 0; i --) 68 tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow); 69 maxflow += tp; 70 for(int i = tail - 1; i >= 0; i --) { 71 edge[sta[i]].flow += tp; 72 edge[sta[i] ^ 1].flow -= tp; 73 if(edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i; 74 } 75 u = edge[sta[tail] ^ 1].to; 76 } 77 else if(cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) { 78 sta[tail ++] = cur[u]; 79 u = edge[cur[u]].to; 80 } 81 else { 82 while(u != s && cur[u] == -1) 83 u = edge[sta[-- tail] ^ 1].to; 84 cur[u] = edge[cur[u]].next; 85 } 86 } 87 } 88 return maxflow; 89 } 90 91 int main() { 92 while(~scanf("%d %d", &p, &n)) { 93 init(); 94 int s = 2 * n + 1, t = s + 1; 95 for(int i = 1; i <= n; i ++) { 96 scanf("%d", &q[i]);//读入某一台机器的工作效率 97 for(int j = 1; j <= p; j ++) scanf("%d", &ps[i][j]);//读入初始状态 98 for(int j = 1; j <= p; j ++) scanf("%d", &pe[i][j]);//读入输出状态 99 } 100 bool flag = true; 101 for(int i = 1; i <= n; i ++) { 102 for(int j = 1; j <= n; j ++) { 103 if(i ^ j) {//如果i != j 104 flag = true; 105 for(int k = 1; k <= p; k ++) {//判断机器j是否可以接受机器i加工之后的状态 106 if(pe[i][k] + ps[j][k] == 1) { 107 flag = false; 108 break; 109 } 110 } 111 if(flag) addedge(i + n, j, inf); 112 } 113 } 114 flag = true; 115 for(int k = 1; k <= p; k ++) {//判断机器i是否为源点 116 if(ps[i][k] % 2 == 1) { 117 flag = false; 118 break; 119 } 120 } 121 if(flag) s1[sizes ++] = i; 122 flag = true; 123 for(int k = 1; k <= p; k ++) {//判断机器i是否为汇点 124 if(pe[i][k] == 0) { 125 flag = false; 126 break; 127 } 128 } 129 if(flag) e1[sizet ++] = i; 130 } 131 for(int i = 0; i < sizes; i ++) addedge(s, s1[i], inf); 132 for(int i = 0; i < sizet; i ++) addedge(n + e1[i], t, inf); 133 for(int i = 1; i <= n; i ++) addedge(i, n + i, q[i]); 134 int maxflow = dinic(s, t, 2 * n + 2); 135 for(int i = 2; i <= tot; i += 2) { 136 if(edge[i].flow > 0 && edge[i].from != s && edge[i].to != t && abs(edge[i].from - edge[i].to) != n) { 137 ans[++ cnt].u = edge[i].from > n ? edge[i].from - n: edge[i].from; 138 ans[cnt].v = edge[i].to > n ? edge[i].to - n : edge[i].to; 139 ans[cnt].w = edges[i].cap - edge[i].cap; 140 // ans[++ cnt] = (node){edge[i].from, edge[i].to, edge[i].flow}; 141 ans[cnt].w = edge[i].flow; 142 } 143 } 144 if(maxflow == 0) cnt = 0; 145 printf("%d %d ", maxflow, cnt); 146 for(int i = 1; i <= cnt; i ++) { 147 printf("%d %d %d ", ans[i].u, ans[i].v, ans[i].w); 148 } 149 } 150 return 0; 151 }