POJ-3436 ACM computer Factory (maximum flow [Ford-fulkerson])

Source: Internet
Author: User

ACM Computer factoryhttp://poj.org/problem?id=3436
Time Limit: 1000MS Memory Limit: 65536K
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, whe Re 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 Sampl  E 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 3 0 0

Hint

Bold texts appearing in the sample sections is informative and does not form part of the actual data.

The main topic: There are N machines can assemble parts of the computer, each machine has a property Q, indicating the number of computers can be processed per hour, each machine can operate on the computer's P parts, the input is a P-tuple, indicating the status of each part, 0 means that the part cannot exist, 1 means that the part must exist, 2 Indicates whether the part is present or not. The output is also a P-tuple, which indicates the output status of each part, 0 indicates that the part is absent, and 1 indicates that the part exists. Add lines first in the need to add lines, such as the output of the ① and the input of the ②, then allow the output of the ① to enter from the ② output (must meet the input standards of ②), Q maximum 1 hours can be assembled to complete how many computers? and the number of lines that need to be added, and how to add them specifically?


First look at the topic, it is very difficult to understand, it is difficult to think of the maximum flow, manually run a few sets of data, the discovery of the topic is to ask from the P-tuple (0,..., 0) to the P-tuple (1,..., 1) The maximum flow, and then you can build a chart to run the maximum flow can

First of all, the input and output of each machine as a point, then there are two points between the flow of Q of the forward Edge

Next, add the edges that may be added, compare the output of each machine to the input of other machines, and if the output conforms to the input standard, add the traffic to an infinite edge

Then run the maximum flow, Note that only the added edges are recorded each time

Final Direct Output Results


There is a problem with the method of starting the statistical path, but a lot of random data is not error, finally found a set of data


Input: 3 275361 1 0 1 0 1 16146 0 1 0 0 0 14141 1 2 0 0 0 15481 2 1 2 1 0 03740 2 2 1 0 0 11473 2 0 0 0 0 11734 2 0 2 1 0 1566 8 2 0 1 1 0 0320 1 1 2 0 0 0564 0 1 1 1 0 12749 0 0 2 0 1 17990 2 1 2 0 0 11042 1 1 2 1 1 02057 0 2 0 1 0 18022 1 2 2 1 1  1664 1 1 0 1 1 0797 1 0 1 1 0 05261 0 0 2 1 1 1557 0 2 0 1 1 14544 0 1 0 1 1 14321 0 2 2 1 1 03596 1 1 1 1 0 07274 0 1 1 0 1 07264 2 2 2 0 1 19165 1 1 2 1 1 14296 2 2 2 0 1 06489 2 0 1 1 0 0 output: 22705 114 15 27586 8 14737 15 17348 15 147311 4 250 111 23 24814 15 205721 25 432123 20 24824 4 25726 20 4296


#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int Sta=1;const int Des=0;const int Inf=0x3f3f3f3f;int P,n,num,q;//num represents the number of States int G[105][105],head,tail,pre[105];int res[55][55];// traffic bool vis[105];struct Status {int p[10] representing machine I to machine J need to create new RES[I][J];} status[105];struct Node {int Pre,u,mn;//pre represents the previous point; U represents the current point; MN represents the minimum traffic}que[2705],cur;bool IsOK for the meeting point to the store (const status&            Sta,const status& des) {//To determine if the edge for (int i=0;i<p;++i) {if (Des.p[i]!=2&&des.p[i]!=sta.p[i]) {        return false; }} return true;    int BFS (int sta,int des) {head=tail=0;    memset (vis,false,sizeof (VIS));    Que[tail].pre=-1;    Que[tail].u=sta;    Que[tail++].mn=inf;        while (Head!=tail) {do {cur=que[head++];        } while (head!=tail&&vis[cur.u]);        if (vis[cur.u]) {//If the queue is empty and there are no points yet to reach the meeting point, there is no augmented path return 0;        } vis[cur.u]=true;        Pre[cur.u]=cur.pre; if (cur.u==des) {return cur.mn;                } for (int i=0;i<num;++i) {if (!vis[i]&&g[cur.u][i]!=0) {que[tail].pre=cur.u;                Que[tail].u=i;            Que[tail++].mn=min (Cur.mn,g[cur.u][i]); }}} return 0;}    int Ford_fulkerson (int sta,int des) {int ans=0,mn,e;        while (Mn=bfs (sta,des), mn!=0) {ans+=mn;        E=des;            while (E!=sta) {g[pre[e]][e]-=mn;            G[E][PRE[E]]+=MN;            if (pre[e]&1) ==1&& (e&1) ==0) {//Only an odd starting point is the output, that is, the forward edge res[pre[e]>>1][e>>1]+=mn;            } else {//reverse edge, then subtract res[e>>1][pre[e]>>1]-=mn; in forward edge        } E=pre[e]; }} return ans;        int main () {while (2==scanf ("%d%d", &p,&n)) {memset (g,0,sizeof (g));        memset (res,0,sizeof (res));        num=2;          for (int i=0;i<p;++i) {status[sta].p[i]=0;//source point all parts are 0  status[des].p[i]=1;//meeting Point all parts are 1} for (int i=1;i<=n;++i) {scanf ("%d", &q);            for (int j=0;j<p;++j) {scanf ("%d", &status[num].p[j]);            } ++num;            for (int j=0;j<p;++j) {scanf ("%d", &status[num].p[j]);            } ++num;        g[num-2][num-1]=q; } for (int i=1;i<=n;++i) {//Add edge if (IsOK (status[sta],status[i<<1])) {//source point connected up to input g [STA]            [i<<1]=inf;                     } for (int j=1;j<=n;++j) {if (I!=j&&isok (status[(j<<1) |1],status[i<<1])) {                g[(j<<1) |1][i<<1]=inf; }} if (IsOK (status[(i<<1) |1],status[des]) {//Reachable output connected to meeting point g[(i<<1) |1][des]=            INF;        }} int Ans=ford_fulkerson (sta,des), cnt=0;      for (int i=1;i<=n;++i) {for (int j=1;j<=n;++j) {          if (res[i][j]>0) {++cnt;        }}} printf ("%d%d\n", ans,cnt); for (int i=1;i<=n;++i) {for (int j=1;j<=n;++j) {if (res[i][j]>0) {PR                INTF ("%d%d%d\n", i,j,res[i][j]); }}}} return 0;}


POJ-3436 ACM computer Factory (maximum flow [Ford-fulkerson])

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.