POJ3436(KB11-A 最大流)
ACM Computer Factory
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8133 | Accepted: 2943 | 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
题意:
电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi。 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移除某些部件)。求电脑公司的单位时间最大产量,以及哪些机器有协作关系,即一台机器把它的产品交给哪些机器加工。
输入:电脑由3个部件组成,共有4台机器,1号机器产量15, 能给空电脑加上2号部件,2号 机器能给空电脑加上2号部件和3号部件, 3号机器能把有1个2号部件和3号部件有无均可的电脑变成成品(每种部件各有一个)
输出:单位时间最大产量25,有两台机器有协作关系,
1号机器单位时间内要将15个电脑给3号机器加工
2号机器单位时间内要将10个电脑给3号机器加工
思路:
拆点建图
网络流模型:
1) 添加一个原点S,S提供最初的原料 00000...
2) 添加一个汇点T, T接受最终的产品 11111...
3) 将每个机器拆成两个点: 编号为i的接收节点,和编号为i+n的产出节点(n是机器数目),前者用于接收原料,后者用于提供加工后的半成品或成品。这两个点之间要连一条边,容量为单位时间产量Qi
4) S 连边到所有接收 "0000..." 或 "若干个0及若干个2" 的机器,容量为无穷大
5) 产出节点连边到能接受其产品的接收节点,容量无穷大
6) 能产出成品的节点,连边到T,容量无穷大。
7) 求S到T的最大流
1 //2017-08-23 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 9 using namespace std; 10 11 const int N = 110; 12 const int P = 20; 13 const int INF = 0x3f3f3f3f; 14 int head[N], tot; 15 struct Edge{ 16 int next, to, w; 17 }edge[N<<4]; 18 19 void add_edge(int u, int v, int w){ 20 edge[tot].w = w; 21 edge[tot].to = v; 22 edge[tot].next = head[u]; 23 head[u] = tot++; 24 25 edge[tot].w = 0; 26 edge[tot].to = u; 27 edge[tot].next = head[v]; 28 head[v] = tot++; 29 } 30 31 struct Dinic{ 32 int level[N], S, T; 33 void init(int _S, int _T){ 34 S = _S; 35 T = _T; 36 tot = 0; 37 memset(head, -1, sizeof(head)); 38 } 39 bool bfs(){ 40 queue<int> que; 41 memset(level, -1, sizeof(level)); 42 level[S] = 0; 43 que.push(S); 44 while(!que.empty()){ 45 int u = que.front(); 46 que.pop(); 47 for(int i = head[u]; i != -1; i = edge[i].next){ 48 int v = edge[i].to; 49 int w = edge[i].w; 50 if(level[v] == -1 && w > 0){ 51 level[v] = level[u]+1; 52 que.push(v); 53 } 54 } 55 } 56 return level[T] != -1; 57 } 58 int dfs(int u, int flow){ 59 if(u == T)return flow; 60 int ans = 0, fw; 61 for(int i = head[u]; i != -1; i = edge[i].next){ 62 int v = edge[i].to, w = edge[i].w; 63 if(!w || level[v] != level[u]+1) 64 continue; 65 fw = dfs(v, min(flow-ans, w)); 66 ans += fw; 67 edge[i].w -= fw; 68 edge[i^1].w += fw; 69 if(ans == flow)return ans; 70 } 71 if(ans == 0)level[u] = 0; 72 return ans; 73 } 74 int maxflow(){ 75 int flow = 0; 76 while(bfs()) 77 flow += dfs(S, INF); 78 return flow; 79 } 80 }dinic; 81 82 int in[N][P], out[N][P]; 83 struct Node{ 84 int u, v, w; 85 Node(int _u, int _v, int _w):u(_u), v(_v), w(_w){} 86 }; 87 88 int main() 89 { 90 std::ios::sync_with_stdio(false); 91 //freopen("inputA.txt", "r", stdin); 92 int n, p, w; 93 while(cin>>p>>n){ 94 int s = 0, t = 2*n+1; 95 dinic.init(s, t); 96 for(int i = 1; i <= n; i++){ 97 cin>>w; 98 add_edge(i, i+n, w); 99 bool fg = true; 100 for(int j = 0; j < p; j++){ 101 cin>>in[i][j]; 102 if(in[i][j] == 1)fg = false; 103 } 104 if(fg)add_edge(0, i, INF); 105 for(int j = 0; j < p; j++) 106 cin>>out[i][j]; 107 } 108 for(int i = 1; i <= n; i++){ 109 bool all_one = true; 110 for(int k = 0; k < p; k++) 111 if(out[i][k] == 0){ 112 all_one = false; 113 break; 114 } 115 if(all_one){ 116 add_edge(i+n, t, INF); 117 } 118 for(int j = 1; j <= n; j++){ 119 if(i == j)continue; 120 bool fg = true; 121 for(int k = 0; k < p; k++){ 122 if((in[j][k] == 1 && out[i][k] == 0) 123 || (in[j][k] == 0 && out[i][k] == 1)){ 124 fg = false; 125 break; 126 } 127 } 128 if(fg)add_edge(i+n, j, INF); 129 } 130 } 131 cout<<dinic.maxflow()<<" "; 132 vector<Node> vec; 133 for(int u = n+1; u < 2*n+1; u++){ 134 for(int i = head[u]; i != -1; i = edge[i].next){ 135 int v = edge[i].to; 136 if(v == t)continue; 137 if(u-n != v && edge[i^1].w != 0){ 138 Node tmp(u-n, v, edge[i^1].w); 139 vec.push_back(tmp); 140 } 141 } 142 } 143 cout<<vec.size()<<endl; 144 for(int i = 0; i < vec.size(); i++) 145 cout<<vec[i].u<<" "<<vec[i].v<<" "<<vec[i].w<<endl; 146 } 147 148 return 0; 149 }