POJ 3436 ACM Computer Factory 最大流

ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6121   Accepted: 2126   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 jDi,k — output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ ≤ 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 andB 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

Hint

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


题意是说,有n个机器。每一个机器有2*p+1个參数值,第一个表示这个机器的最大容量,就是最大可加工零件的数量,然后的p个參数表示。输入条件控制,再之后的p个參数表示输出情况。在输入部分有三种数字,0。1,2,表示对可以送入当前第i个机器加工的零件的P个部分的要求,0表示这个部分一定不能有。1表示这个部分一定要有。2表示这个位置可有可无。

对于输出部分。仅仅有两种状态就是0和1。0表示这个位置输出时没有部件。1表示这个位置输出时有部件。

最后输出时,仅仅有当输出部分p个位置都是1,才算完毕。

那么想要形成一个流水线,即产品从一个机器出来,再从还有一个机器进去,要满足的条件是,前一个机器的输出必须满足后一个机器的输入。那么怎么才算满足呢。因为2比較特别能够在必要时候看成0或者1,所以能够无论它。

那么对于0和1,00 。11的时候肯定是满足的。然而对于01或者10就不能满足了。于是,仅仅要前一个机器的后P位中不论什么一位和后一个机器的前P位中相应位置相加等于1,那么这两个机器就无法连边。

让全部能连接起来的机器连成一张图,题目要求一次最多能加工多少产品而且输出最大流的路径。


#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 109
#define INF 99999999
using namespace std;

int cap[N][N];//最大容量
int flow[N][N];//实际流量
int mp[N][N];//满足条件的机器则连边建图

int n;

int EK(int s,int t)
{
    int pre[N],a[N];
    memset(flow,0,sizeof flow);
    pre[s]=-1;

    queue<int>q;

    int ans=0;
    while(1)
    {
        memset(a,0,sizeof a);
        a[s]=INF;

        q.push(s);
        while(!q.empty())
        {
            int v=q.front();q.pop();
            for(int i=1;i<n;i++)
            {
                if(a[i]==0 && cap[v][i]>flow[v][i])
                {
                    a[i]=min(a[v],cap[v][i]-flow[v][i]);
                    pre[i]=v;
                    q.push(i);
                }
            }
            //if(a[t]) break;
        }

        if(a[t]==0) break;
       for(int i=pre[t],j=t;i!=-1;j=i,i=pre[i])
       {
           flow[i][j]+=a[t];
           flow[j][i]-=a[t];
       }
       ans+=a[t];
    }

    return ans;
}

int main()
{
    int p;
    int s,t;//源点和汇点

    while(~scanf("%d%d",&p,&n))
    {
       s=0;t=n+1;
       for(int i=0;i<2*p+1;i++)//初始化源点和汇点
       {
           mp[0][i]=0;
           mp[n+1][i]=1;
       }

       for(int i=1;i<=n;i++)
       for(int j=0;j<2*p+1;j++)
       {
           scanf("%d",&mp[i][j]);
       }

       n+=2;
       memset(cap,0,sizeof cap);

       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               if(i==j) continue;
               int flag=0;
               for(int k=1;k<=p;k++)
               {
                   if(mp[i][k+p]+mp[j][k]==1)//输出和输入的和等于1则不符合
                   {
                       flag=1;break;
                   }
               }
               if(flag==1) continue;
               if(i==0) cap[i][j]=mp[j][0];
               else if(j==n-1) cap[i][j]=mp[i][0];
               else cap[i][j] += min(mp[i][0],mp[j][0]); //注意一定是多条流的和,加等于

           }
       }

       printf("%d ",EK(0,n-1));

       int cnt=0;
       for(int i=1;i<n-1;i++)
        for(int j=1;j<n-1;j++)
        if(flow[i][j]>0)
            cnt++;

       cout<<cnt<<endl;
       for(int i=1;i<n-1;i++)
       for(int j=1;j<n-1;j++)
       if(flow[i][j]>0)
       printf("%d %d %d
",i,j,flow[i][j]);

    }
    return 0;
}










原文地址:https://www.cnblogs.com/claireyuancy/p/6937025.html