POJ 3436 ACM Computer Factory 最大流

來源:互聯網
上載者:User

標籤:

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 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

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 99999999using 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\n",i,j,flow[i][j]);    }    return 0;}










著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

POJ 3436 ACM Computer Factory 最大流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.