描述
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迷惑了)
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 <set> 11 #include <vector> 12 #define maxn 105 13 #define inf 999999 14 #define EPS 1e-10 15 #define lowbit(x) (int)(x&(-x)) 16 using namespace std; 17 18 int G[maxn][maxn],_G[maxn][maxn]; 19 bool visited[maxn]; 20 int Layer[maxn]; 21 int p, n, m; 22 int all[maxn][20]; 23 24 bool countlayer() { 25 int layer = 0; 26 deque<int>q; 27 memset(Layer, 0xff, sizeof(Layer)); 28 Layer[0] = 0; 29 q.push_back(0); 30 while (!q.empty()) { 31 int v = q.front(); 32 q.pop_front(); 33 for(int j=1;j<=m;j++) 34 if (G[v][j] > 0 && Layer[j] == -1) { 35 Layer[j] = Layer[v] + 1; 36 if (j == m)return true; 37 else q.push_back(j); 38 } 39 } 40 return false; 41 } 42 43 int Dicnic() { 44 int i, nmaxflow = 0; 45 deque<int>q; 46 while (countlayer()) { 47 q.push_back(0); 48 memset(visited, 0, sizeof(visited)); 49 visited[0] = 1; 50 while (!q.empty()) { 51 int nd = q.back(); 52 if (nd == m) { 53 int nminc = inf; 54 int nminc_vs; 55 for (i = 1; i < q.size(); i++) { 56 int vs = q[i - 1]; 57 int ve = q[i]; 58 if (G[vs][ve] > 0) { 59 if (nminc > G[vs][ve]) { 60 nminc = G[vs][ve]; 61 nminc_vs = vs; 62 } 63 } 64 } 65 nmaxflow += nminc; 66 for (i = 1; i < q.size(); i++) { 67 int vs = q[i - 1]; 68 int ve = q[i]; 69 G[vs][ve] -= nminc; 70 G[ve][vs] += nminc; 71 } 72 while (!q.empty() && q.back() != nminc_vs) { 73 visited[q.back()] = 0; 74 q.pop_back(); 75 } 76 } 77 else { 78 for (i = 1; i <= m; i++) { 79 if (G[nd][i] > 0 && Layer[i] == Layer[nd] + 1 &&!visited[i]) { 80 visited[i] = 1; 81 q.push_back(i); 82 break; 83 } 84 } 85 if (i > m) 86 q.pop_back(); 87 } 88 } 89 } 90 return nmaxflow; 91 } 92 93 bool eq(int x, int y) { 94 for (int i = 1; i <= p; i++) { 95 if (all[x][i] == 0 && all[y][i] == 1 || 96 all[x][i] == 1 && all[y][i] == 0) 97 return false; 98 } 99 return true; 100 } 101 102 void pp() { 103 int cnt = 0; 104 for (int i = 1; i <= n; i++) 105 for (int j = 1; j <= n; j++) { 106 int ans = inf - G[2 * i][2 * j - 1]; 107 if (ans > 0 && ans <= _G[2 * i][2 * j - 1]) 108 cnt++; 109 } 110 printf(" %d ", cnt); 111 for(int i=1;i<=n;i++) 112 for (int j = 1; j <= n; j++) { 113 int ans = inf - G[2 * i][2 * j - 1]; 114 if (ans > 0&&ans<=_G[2*i][2*j-1]) 115 printf("%d %d %d ", i, j, ans); 116 } 117 } 118 119 void init() { 120 scanf("%d%d", &p, &n); 121 for (int i = 1; i <= 2*n; i+=2) { 122 scanf("%d", &G[i][i + 1]); 123 _G[i][i + 1] = G[i][i + 1]; 124 for (int j = 1; j <= p; j++) 125 scanf("%d", &all[i][j]); 126 for (int j = 1; j <= p; j++) 127 scanf("%d", &all[i+1][j]); 128 } 129 m = 2 * n + 1; 130 for (int i = 1; i <= p; i++) 131 all[m][i] = 1; 132 for (int i = 1; i <= 2 * n; i += 2) { 133 if (eq(0, i)) 134 G[0][i] = inf; 135 if (eq(i + 1, m)) 136 G[i+1][m] = inf; 137 for (int j = 1; j <= 2 * n; j+=2) { 138 if (i == j)continue; 139 if (!eq(j+1,m)&&eq(j + 1, i)) { 140 G[j + 1][i] = inf; 141 _G[j + 1][i] = G[j + 1][i]; 142 } 143 if (!eq(i+1,m)&&eq(i + 1, j)) { 144 G[i + 1][j] = inf; 145 _G[i + 1][j] = G[i + 1][j]; 146 } 147 } 148 } 149 printf("%d", Dicnic()); 150 pp(); 151 } 152 153 int main() { 154 init(); 155 return 0; 156 }
更新了正确的题解↑