POJ 3436 ACM Computer Factory (maximum flow)

Source: Internet
Author: User

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6773 Accepted: 2379 Special Judge

Description

As you know, all the computers used for ACM contests must is identical, so the participants compete on equal terms. That's why all these computers was historically produced at the same factory.

Every ACM computer consists of P parts. When all these parts was 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. Removes some parts from a half-finished computer and adds some new parts (removing of parts are sometimes nece Ssary as the parts cannot is added to a computer in arbitrary order). Each machine was 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 being able to operat E on it. The specification is a set of P numbers 0, 1 or 2 (one number to each part), where 0 means that corresponding PA RT must not being present, 1-the part was 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 was absent, 1-the part is present.

The machines connected by very fast production lines so, 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 G Rowing contest needs. That's 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 FA Ctory machines. It was noted that the easiest-to upgrade the factory are to rearrange production lines. ACM Directorate decided to entrust and solving this problem.

Input

Input file contains integers P n, then n descriptions of the machines. The description of ith machine was 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 is made, then M descriptions of the connections. Each connection between machines a and B must is 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  0 1 2  1  1 0 2 1 1 1 1Sample input 23   0 0 0  0 1 0100 0 1 0 1 0 0 1 0  1 1  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 13 3sample Output 24, 3 5, 2, 4, 5 1Sample output 30 0

Analysis
Test instructions is a computer has p parts, there are N machines, each machine can only process the specified parts of the semi-finished computer, and then add or subtract some parts of the computer, each machine has a performance, the last is to find the largest performance and

There are three types of input specifications: 0,1,2

0: The part cannot exist

1: This section must be retained

2: This part is optional

There are 2 types of output specifications: 0,1

0: The part does not exist

1: This Part exists

is to seek the maximum flow, the key is to build a diagram, first each machine has a performance, so each machine is split into two points, the middle with a Benquan for the W of the connected line;

The input specifications are all 0 connected to the source point, all 1 and the meeting point. Two machines I and J, if the outputs of I meet the input of J, they are connected and take a smaller flow.

Then run the maximum flow on the line.

The output path first saves the graph before the maximum flow, and after the comparison, if small, it means that the two points are the points on the path, save up the output will be OK.

#include <stdio.h>#include<string.h>#include<iostream>#include<queue>using namespacestd;//****************************************************//maximum flow template Edmonds_karp algorithm//initialization: G[][],st,ed//******************************************************Const intMAXN = $+Ten;Const intINF =0x3fffffff;intG[MAXN][MAXN];//capacity of the storage edge, no edge initialized to 0intpath[maxn],flow[maxn],st,ed;intN//Number of points, 0~n,n includes source and sink pointsQueue<int>Q;intBFs () {inti,t;  while(!q.empty ()) Q.pop ();//Empty Queuememset (path,-1,sizeof(path));//initialize the path to 1 before each searchpath[st]=0; FLOW[ST]=inf;//The source point can have infinite stream flow intoQ.push (ST);  while(!Q.empty ()) {T=Q.front ();        Q.pop (); if(t==ed) Break;  for(i=0; i<=n;i++){            if(i!=st&&path[i]==-1&&G[t][i]) {Flow[i]=flow[t]<g[t][i]?Flow[t]:g[t][i];                Q.push (i); Path[i]=T; }        }    }    if(path[ed]==-1)return-1;//I can't find the meeting point up there. I can't find the augmented path.    returnflow[ed];}intEdmonds_karp () {intmax_flow=0; intStep,now,pre;  while((Step=bfs ())!=-1) {Max_flow+=step; now=Ed;  while(now!=St) {Pre=Path[now]; G[pre][now]-=step; G[now][pre]+=step; now=Pre; }    }    returnMax_flow;}int inch[MAXN] [ -];//Enter informationintBACKUP[MAXN][MAXN];//Backup Diagramintline[maxn][4];intMain () {intP,n;  while(SCANF ("%d%d", &p,&n)! =EOF) {memset (G,0,sizeof(G));  for(intI=1; i<=n;i++)             for(intj=0;j<2*p+1; j + +) scanf ("%d",&inch[i][j]);  for(intI=1; i<=n;i++)//Split Pointg[2*i-1][2*i]=inchI [0]; N=2*n+1; St=0, ed=N;  for(intI=1; i<=n;i++){            BOOLflag_s=true; BOOLflag_t=true;  for(intj=1; j<=p;j++){                if(inchI [j]==1) flag_s=false; if(inchI [j+p]==0) flag_t=false; }            if(flag_s) g[st][2*i-1]=INF; if(flag_t) g[2*i][ed]=INF;  for(intj=1; j<=n;j++){                if(j==i)Continue; BOOLflag=true;  for(intk=1; k<=p;k++)                    if((inchI [k+p]==0&&inch[j] [k]==1)|| (inchI [k+p]==1&&inch[j] [k]==0) ) {flag=false;  Break; }                if(flag) g[2*i][2*j-1]=min (inchI [0],inch[j] [0]); }} memcpy (Backup,g,sizeof(G)); printf ("%d", Edmonds_karp ()); intTol=0;  for(intI=1; i<=n;i++){             for(intj=1; j<=n;j++){                if(g[2*i][2*j-1]<backup[2*i][2*j-1]) {line[tol][0]=i; line[tol][1]=J; line[tol][2]=backup[2*i][2*j-1]-g[2*i][2*j-1]; Tol++; }}} printf ("%d\n", Tol);  for(intI=0; i<tol;i++) printf ("%d%d%d\n", line[i][0],line[i][1],line[i][2]); }    return 0;}

POJ 3436 ACM Computer Factory (maximum flow)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.