18.11.23 POJ 3436 ACM Computer Factory(dinic)
Posted yalphait
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了18.11.23 POJ 3436 ACM Computer Factory(dinic)相关的知识,希望对你有一定的参考价值。
描述
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 Nvarious 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 Pnumbers 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 Pnumbers 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 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 Qispecifies 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 the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines Aand 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 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 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
提示
Bold texts appearing in the sample sections are informative and do not form part of the actual data.来源Northeastern Europe 2005, Far-Eastern Subregion
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 #include <queue> 8 #include <stdio.h> 9 #include <string.h> 10 #include <vector> 11 #include <fstream> 12 #include <set> 13 #define maxn 105 14 #define inf 0x7fffffff 15 16 using namespace std; 17 struct node { 18 int oldstatus[11], newstatus[11]; 19 node() { 20 memset(oldstatus, 0, sizeof(oldstatus)); 21 memset(newstatus, 0, sizeof(newstatus)); 22 } 23 }; 24 node _node[maxn]; 25 int G[maxn][maxn],Q[maxn]; 26 int p, n; 27 int Layer[maxn]; 28 int flow[maxn][maxn]; 29 30 bool countlayer() { 31 int layer = 0; 32 deque<int>q; 33 memset(Layer, 0xff, sizeof(Layer)); 34 Layer[0] = 0; 35 q.push_back(0); 36 while (!q.empty()) { 37 int v = q.front(); 38 q.pop_front(); 39 for (int j = 0; j <= n + 1; j++) { 40 if (G[v][j] > 0 && Layer[j] == -1) { 41 Layer[j] = Layer[v] + 1; 42 if (j == n+1)return true; 43 else q.push_back(j); 44 } 45 } 46 } 47 return false; 48 } 49 50 bool visited[maxn]; 51 void dicnic() { 52 int MaxFlow = 0; 53 deque<int>q; 54 while (countlayer()) { 55 q.push_back(0); 56 memset(visited, 0, sizeof(visited)); 57 visited[0] = true; 58 while (!q.empty()) { 59 int nd = q.back(); 60 if (nd == n + 1) { 61 int nMinC = inf, Min_vs; 62 for (int i = 1; i < q.size(); i++) { 63 int vs = q[i - 1], ve = q[i]; 64 if (G[vs][ve] > 0) { 65 if (nMinC > G[vs][ve]) { 66 nMinC = G[vs][ve]; 67 Min_vs = vs; 68 } 69 } 70 } 71 MaxFlow += nMinC; 72 for (int i = 1; i < q.size(); i++) { 73 int vs = q[i - 1], ve = q[i]; 74 G[vs][ve] -= nMinC; 75 G[ve][vs] += nMinC; 76 flow[vs][ve] += nMinC; 77 } 78 while (!q.empty() && q.back() != Min_vs) { 79 visited[q.back()] = false; 80 q.pop_back(); 81 } 82 } 83 else { 84 int i; 85 for (i = 0; i <= n + 1; i++) { 86 if (G[nd][i] > 0 && Layer[i] == Layer[nd] + 1 && !visited[i]) { 87 visited[i] = true; 88 q.push_back(i); 89 break; 90 } 91 } 92 if (i > n + 1) 93 q.pop_back(); 94 } 95 } 96 } 97 printf("%d", MaxFlow); 98 } 99 100 void init() { 101 scanf("%d%d", &p, &n); 102 for (int i = 1; i <= n; i++) { 103 scanf("%d", &Q[i]); 104 bool flag = true; 105 for (int j = 1; j <= p; j++) 106 { 107 scanf("%d", &_node[i].oldstatus[j]); 108 if (_node[i].oldstatus[j] == 1) 109 flag = false; 110 } 111 if (flag)G[0][i] = Q[i]; 112 flag = true; 113 for (int j = 1; j <= p; j++) 114 { 115 scanf("%d", &_node[i].newstatus[j]); 116 if (_node[i].newstatus[j] != 1)flag = false; 117 } 118 if (flag)G[i][n+1] = Q[i]; 119 } 120 for(int i=1;i<=n;i++) 121 for (int j = 1; j <= n; j++) { 122 { 123 if (i == j)continue; 124 bool flag = true; 125 for (int k = 1; k <= p; k++) 126 if (_node[i].oldstatus[k] == 1 && _node[j].newstatus[k] == 0 127 || _node[i].oldstatus[k] == 0 && _node[j].newstatus[k] == 1) { 128 flag = false; 129 break; 130 } 131 if (flag) 132 G[j][i] = Q[j]; 133 } 134 } 135 dicnic(); 136 int sum = 0; 137 for(int i=1;i<=n;i++) 138 for (int j = 1; j <= n; j++) { 139 if (i == j)continue; 140 int res = flow[i][j]-flow[j][i]; 141 if (res > 0) 142 sum++; 143 } 144 printf(" %d ", sum); 145 for (int i = 1; i <= n; i++) 146 for (int j = 1; j <= n; j++) { 147 if (i == j)continue; 148 int res = flow[i][j] - flow[j][i]; 149 if (res > 0) 150 printf("%d %d %d ", i, j, res); 151 } 152 } 153 154 int main() 155 { 156 init(); 157 return 0; 158 }
其实是数据太水了,本来要拆点的,被我蒙过去了,其实不拆不行的(x
并且 我dinic写错了(x
写的时候懒了那么一下(并且被最后那张ppt迷惑了)
以上是关于18.11.23 POJ 3436 ACM Computer Factory(dinic)的主要内容,如果未能解决你的问题,请参考以下文章
POJ - 3436 ACM Computer Factory 网络流
POJ 3436 ACM Computer Factory (最大流)