最大流 poj 3436

来源:互联网 发布:csmhuan实时数据 编辑:程序博客网 时间:2024/06/06 00:17
ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4145 Accepted: 1391 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 13 415  0 0 0  0 1 010  0 0 0  0 1 130  0 1 2  1 1 13   0 2 1  1 1 1Sample input 23 55   0 0 0  0 1 0100 0 1 0  1 0 13   0 1 0  1 1 01   1 0 1  1 1 0300 1 1 2  1 1 1Sample input 32 2100  0 0  1 0200  0 1  1 1

Sample Output

Sample output 125 21 3 152 3 10Sample output 24 51 3 33 5 31 2 12 4 14 5 1Sample output 30 0


题意:
工厂中加工零件,首先输入加工零件的个数 n,以及机器的个数 m。
接下来的m行有2n+1个数,分别表示每台机器的效率以及这台机器要生产零件需要零件的状态以及产生零件的状态。
其中状态中,1表示该部分要求完成或已完成,0表示该部分不需要要完成或没有完成,2只表示需要零件的状态,表示该部分完不完成都满足。
求完成零件生产的最大效率。
题解:
将图变形后,即是最大流的题目;
将每台机器都看做一个点,该机器的效率即为该点的权值,如果一台机器的结果刚好满足第二台机器的前提,那么存在一条从第一台机器到第二台机器的边。
做好图后,先创建一个源点,将所有前提状态为不需要任何已完成条件的点与之相连,然后,再创建一个终点,使产生的零件为已完成状态的点与之相连;并且两种情况的边上的值都为无穷大。
然后进行拆点,把每个点拆成两个点,两点间的边的值为点的权值;本来就存在边的两点,将边的尾放在拆成的第二个点,将边的头放在拆成的点的第一个点,这样一来,图就构好啦。


代码实现:
#include<iostream>#include<stdio.h>#include<string.h>//#include<queue>#define INF 1000000000using namespace std;int cup[200][200]; int copyc[200][200];            //the max number the way allowedint a[200];                       //a[i]:from start point to i's minnum allow numberint b1[100][20];int b2[100][20];int fa[200]; struct node{       int start,end;       int w;};node way[200];                //fa[i]: in a way,the point before iint main(){    int n,m;                        //cin hte point's number and way's number    while(cin>>n>>m){        memset(cup,0,sizeof(cup));          memset(copyc,0,sizeof(copyc));        memset(b1,0,sizeof(b1));        memset(b2,0,sizeof(b2));                           for(int i=1;i<=m;i++){                 int temp1;                scanf("%d",&temp1);                 cup[i][i+m]=temp1;                cup[i+m][i]=temp1;                int flag1=0;                int flag2=0;                for(int j=1;j<=n;j++){                     scanf("%d",&b1[i][j]);                     if(b1[i][j]==1){                         flag1=1;                         }                }               for(int k=1;k<=n;k++){                       scanf("%d",&b2[i][k]);                       if(b2[i][k]==0)                           flag2=1;                           }               if(flag1==0){                        cup[0][i]=INF;                        cup[i][0]=INF;                        }               if(flag2==0){                        cup[i+m][2*m+1]=INF;                        cup[2*m+1][i+m]=INF;                        }                        }               for(int k=1;k<=m;k++){                       for(int g=1;g<=m;g++){                               if(k!=g){                                  int i;                                  for(i=1;i<=n;i++){                                          if(b1[k][i]+b2[g][i]==1)                                                break;                                                }                                  if(i>n){                                          cup[g+m][k]=INF;                                          }                                  int j;                                  for(j=1;j<=n;j++){                                          if(b2[k][j]+b1[g][j]==1){                                                break;                                                }                                                }                                 if(j>n)                                          cup[k+m][g]=INF;                                          }                                          }                                          }        memcpy(copyc,cup,sizeof(cup));          int sum=0;        int times=0;        while(true){        //queue <int> q;          int que[500];          int first=0,last=0;          memset(a,0,sizeof(a));          memset(fa,0,sizeof(fa));          que[last++]=0;          //q.push(0);          a[0]=INF;          int flag=0;          while(first!=last){              int p=que[first++];                     //get a possble way from start to end             //int p=q.front();             //q.pop();             for(int j=1;j<=2*m+1;j++){                     if(a[j]==0&&cup[p][j]>0){                            //cout<<cup[p][j]<<" "<<flow[p][j]<<endl;                            fa[j]=p;                            a[j]=a[p]>(cup[p][j])?(cup[p][j]):a[p];    // get the way's minnum allow number                            if(j==2*m+1){                                 flag=1;                                 break;                                 }                            que[last++]=j;                           // q.push(j);                            }                            }          if(flag==1)                break;                }          if(a[2*m+1]==0)                                //cannot to end,means the possible is unexist                break;          for(int k=2*m+1;k!=0;k=fa[k]){                      //find a way, change its cross number now                  cup[fa[k]][k]-=a[2*m+1];                  cup[k][fa[k]]+=a[2*m+1];                     //the maxn number the graph can cross          }          sum+=a[2*m+1];          }        for(int i=m+1;i<=2*m;i++){                for(int j=1;j<=m;j++){                        if(i!=j&&copyc[i][j]>cup[i][j]){                                 times++;                                 }                                 }                                 }        cout<<sum<<" "<<times<<endl;        for(int g=m+1;g<=2*m;g++){                for(int k=1;k<=m;k++){                if(copyc[g][k]>cup[g][k])                   cout<<g-m<<" "<<k<<" "<<copyc[g][k]-cup[g][k]<<endl;                }                }        }    return 0;}