POJ-3436 ACM Computer Factory (最大流[Ford-Fulkerson])

ACM Computer Factory
http://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 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 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

Hint

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

题目大意:有n台机器能组装电脑部分零件,每台机器有属性q,表示每小时能处理的电脑数,每台机器可对电脑的p个零件进行操作,输入为p元组,表示每个零件的状态,0表示不能存在该零件,1表示必须存在该零件,2表示该零件存在与否均可。输出也为p元组,表示每个零件的输出状态,0表示不存在该零件,1表示存在该零件。先在需要添加线路,例如在①的输出和②的输入添加线路,则允许①的输出从②的输出进入(必须满足②的输入标准),问最大1小时能组装完成多少台电脑?以及需要添加的线路数,及具体的添加方法?


初看题目,感觉非常难懂,也很难想到最大流,手动跑了几组数据后,发现题目就是求从p元组(0,...,0)到p元组(1,...,1)的最大流,然后就可以建图跑最大流即可

首先,将每台机器的输入输出分别看成1个点,则这两点之间存在流量为q的有向边

其次,添加可能会添加的边,即将各机器的输出与其他机器的输入比较,如果输出符合输入标准,则添加流量为无穷的边

然后跑最大流即可,注意每次只记录添加的边即可

最后直接输出结果


刚开始统计路径的方法有点问题,但是随机了很多数据都找不出错,最后终于找到一组数据


输入:
3 27
5361 1 0 1 0 1 1
6146 0 1 0 0 0 1
4141 1 2 0 0 0 1
5481 2 1 2 1 0 0
3740 2 2 1 0 0 1
1473 2 0 0 0 0 1
1734 2 0 2 1 0 1
5668 2 0 1 1 0 0
320 1 1 2 0 0 0
564 0 1 1 1 0 1
2749 0 0 2 0 1 1
7990 2 1 2 0 0 1
1042 1 1 2 1 1 0
2057 0 2 0 1 0 1
8022 1 2 2 1 1 1
664 1 1 0 1 1 0
797 1 0 1 1 0 0
5261 0 0 2 1 1 1
557 0 2 0 1 1 1
4544 0 1 0 1 1 1
4321 0 2 2 1 1 0
3596 1 1 1 1 0 0
7274 0 1 1 0 1 0
7264 2 2 2 0 1 1
9165 1 1 2 1 1 1
4296 2 2 2 0 1 0
6489 2 0 1 1 0 0
输出:
22705 11
4 15 2758
6 8 1473
7 15 1734
8 15 1473
11 4 2501
11 23 248
14 15 2057
21 25 4321
23 20 248
24 4 257
26 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表示状态的个数
int g[105][105],head,tail,pre[105];
int res[55][55];//表示机器i到机器j需要新建res[i][j]的流量
bool vis[105];

struct Status {
    int p[10];
}status[105];

struct Node {
    int pre,u,mn;//pre表示上一个点;u表示当前点;mn表示汇点到该店的最小流量
}que[2705],cur;

bool isOK(const Status& sta,const Status& des) {//判断是否能够连边
    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]) {//如果队列为空还没有未到的点,则无法到达汇点,则不存在增广路径
            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) {//只有为奇数的起点才是output,即正向边
                res[pre[e]>>1][e>>1]+=mn;
            }
            else {//反向边,则要在正向边中减去
                res[e>>1][pre[e]>>1]-=mn;
            }
            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;//源点所有部件均为0
            status[DES].p[i]=1;//汇点所有部件均为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) {//添加边
            if(isOK(status[STA],status[i<<1])) {//源点连上可达的输入
                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])) {//可达的输出连上汇点
                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) {
                    printf("%d %d %d\n",i,j,res[i][j]);
                }
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值