ACM Computer Factory
64-bit integer IO format: %lld Java class name: Main
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int INF = ~0U>>2; 6 const int maxn = 200; 7 struct arc { 8 int to,flow,next; 9 arc(int x = 0,int y = 0,int z = -1) { 10 to = x; 11 flow = y; 12 next = z; 13 } 14 } e[maxn*maxn]; 15 int head[maxn],d[maxn],gap[maxn],tot,S,T,n; 16 void add(int u,int v,int flow) { 17 e[tot] = arc(v,flow,head[u]); 18 head[u] = tot++; 19 e[tot] = arc(u,0,head[v]); 20 head[v] = tot++; 21 } 22 int dfs(int u,int low) { 23 if(u == T) return low; 24 int tmp = 0,minH = n - 1; 25 for(int i = head[u]; ~i; i = e[i].next) { 26 if(e[i].flow) { 27 if(d[u] == d[e[i].to] + 1) { 28 int a = dfs(e[i].to,min(low,e[i].flow)); 29 e[i].flow -= a; 30 e[i^1].flow += a; 31 tmp += a; 32 low -= a; 33 if(!low) break; 34 if(d[S] >= n) return tmp; 35 } 36 if(e[i].flow) minH = min(minH,d[e[i].to]); 37 } 38 } 39 if(!tmp) { 40 if(--gap[d[u]] == 0) d[S] = n; 41 ++gap[d[u] = minH + 1]; 42 } 43 return tmp; 44 } 45 int in[maxn][maxn],ou[maxn][maxn],P,N; 46 bool check(int a,int b) { 47 for(int i = 0; i < P; ++i) { 48 if(in[b][i] < 2 && ou[a][i] != in[b][i]) return false; 49 } 50 return true; 51 } 52 int main() { 53 while(~scanf("%d%d",&P,&N)) { 54 memset(head,-1,sizeof head); 55 memset(d,0,sizeof d); 56 memset(gap,0,sizeof gap); 57 for(int i = tot = 0,tmp; i < N; ++i) { 58 scanf("%d",&tmp); 59 add(i,i + N,tmp); 60 for(int j = 0; j < P; ++j) 61 scanf("%d",in[i] + j); 62 for(int j = 0; j < P; ++j) 63 scanf("%d",ou[i] + j); 64 } 65 S = N + N; 66 T = S + 1; 67 n = T + 1; 68 for(int i = 0; i < N; ++i) { 69 bool none = true,allone = true; 70 for(int j = 0; j < P; ++j) { 71 if(in[i][j] == 1) none = false; 72 if(ou[i][j] != 1) allone = false; 73 } 74 if(none) add(S,i,INF); 75 if(allone) add(i + N,T,INF); 76 for(int j = 0; j < N; ++j) { 77 if(i == j) continue; 78 if(check(i,j)) add(i + N,j,INF); 79 } 80 } 81 int ret = 0,cnt = 0; 82 gap[S] = n; 83 while(d[S] < n) ret += dfs(S,INF); 84 85 for(int i = 0; i < N; ++i) { 86 for(int j = head[i + N]; ~j; j = e[j].next) { 87 if(e[j].to < N && e[j].to != i && e[j^1].flow) ++cnt; 88 } 89 } 90 printf("%d %d ",ret,cnt); 91 for(int i = 0; i < N; ++i) { 92 for(int j = head[i + N]; ~j; j = e[j].next) { 93 if(e[j].to < N && e[j].to != i && e[j^1].flow) 94 printf("%d %d %d ",i + 1,e[j].to + 1,e[j^1].flow); 95 } 96 } 97 } 98 return 0; 99 }