Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4073 | Accepted: 1363 | 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 ofP 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 ofith machine is represented as by 2 P + 1 integers Qi Si,1Si,2...Si,P Di,1Di,2...Di,P, whereQi specifies performance, Si,j — input specification for partj, Di,k — output specification for partk.
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, thenM descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, whereW 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
#include<iostream> #include<cstdio> using namespace std; #define MAXN 200 #define INF 0xFFFFFF struct edge { int to,c,next; }e[99999]; struct edge2 { int u,v,f; }ee[99999]; int pi[15][MAXN],po[15][MAXN]; int que[1000], dis[MAXN],pre[MAXN]; int head[MAXN],head2[MAXN]; int st,ed,maxflow; int en; int n,p; void add(int a,int b,int c) { e[en].to=b; e[en].c=c; e[en].next=head[a]; head[a]=en++; e[en].to=a; e[en].c=0; e[en].next=head[b]; head[b]=en++; } bool bfs() { memset(dis,-1,sizeof(dis)); que[0]=st,dis[st]=1; int t=1,f=0; while(f<t) { int j=que[f++]; for(int k=head[j];k!=-1;k=e[k].next) { int i=e[k].to; if(dis[i]==-1 && e[k].c) { que[t++]=i; dis[i]=dis[j]+1; if(i==ed) return true; } } } return false; } int update() { int p,flow=INF; for (int i=pre[ed];i!=-1;i=pre[i]) if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c; for (int i=pre[ed];i!=-1;i=pre[i]) e[head2[i]].c-=flow,e[head2[i]^1].c+=flow; maxflow+=flow; return p; } void dfs() { memset(pre,-1,sizeof(pre)); memcpy(head2,head,sizeof(head2)); for(int i=st,j;i!=-1;) { int flag=false; for(int k=head[i];k!=-1;k=e[k].next) if(e[k].c && (dis[j=e[k].to]==dis[i]+1) ) { pre[j]=i; head2[i]=k; i=j; flag=true; if(i==ed) i=update(); if(flag) break; } if (!flag) dis[i]=-1,i=pre[i]; } } void solve() { bool flag=true; int temp; st=0,ed=n*2+1; en=0; maxflow=0; memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) { scanf("%d",&temp); add(i,i+n,temp); flag=true; for(int j=1;j<=p;j++) { scanf("%d",&pi[i][j]); if(pi[i][j]==1) flag=false; } if(flag) add(st,i,INF); flag=true; for(int j=1;j<=p;j++) { scanf("%d",&po[i][j]); if(!po[i][j]) flag=false; } if(flag) add(i+n,ed,INF); } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { flag=true; if(i==j) continue; for(int k=1;k<=p;k++) { if(po[i][k]==1 && pi[j][k]==0) flag=false; if(po[i][k]==0 && pi[j][k]==1) flag=false; if(!flag) break; } if(flag) add(i+n,j,INF); } } while(bfs()) dfs(); int counts=0; for(int i=1;i<=n;i++) { for(int j=head[i+n];j!=-1;j=e[j].next) { if(e[j].to==i || e[j].to==ed) continue; if(e[j^1].c) { ee[counts].u=i; ee[counts].v=e[j].to; ee[counts++].f=e[j^1].c; } } } printf("%d %d\n",maxflow,counts); for(int i=0;i<counts;i++) printf("%d %d %d\n",ee[i].u,ee[i].v,ee[i].f); } int main() { while(scanf("%d%d",&p,&n)!=EOF) solve(); return 0; }