最大流 poj 3436

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total 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 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


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


代码实现:
#include<iostream>
#include<stdio.h>
#include<string.h>
//#include<queue>
#define INF 1000000000
using namespace std;
int cup[200][200]; 
int copyc[200][200];            //the max number the way allowed
int a[200];                       //a[i]:from start point to i's minnum allow number
int 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 i
int 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值