POJ 3436 ACM Computer Factory(最大流,拆点,dinic)

ACM Computer Factory

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9440 Accepted: 3505 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.

/*
输入:电脑由3个部件组成,共有4台机器,1号机器产量15, 能给空电脑加上2号部件,2号 机器能给空电脑加上2号部件和3号部件, 3号机器能把有1个2号部件和3号部件有无均可的电脑变成成品(每种部件各有一个)
输出:单位时间最大产量25,有两台机器有协作关系,
1号机器单位时间内要将15个电脑给3号机器加工
2号机器单位时间内要将10个电脑给3号机器加工

思路:
网络流模型:
(1) 添加一个原点S,S提供最初的原料 00000…
(2) 添加一个汇点T, T接受最终的产品 11111…
(3) 将每个机器拆成两个点: 编号为i的接收节点,和编号为i+n的产出节点(n是机器数目),前者用于接收原料,后者用于提供加工后的半成品或成品。这两个点之间要连一条边,容量为单位时间产量Qi
(4) S 连边到所有接收 0000”或 “若干个0及若干个2” 的机器,容量为无穷大
(5) 产出节点连边到能接受其产品的接收节点,容量无穷大
(6) 能产出成品的节点,连边到T,容量无穷大。
(7) 求S到T的最大流
*/

#include <iostream>
#include <cstring>
#include <queue>
#include <string>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
#define ll long long
#define pi acos(-1.0)
#define maxx 505
#define inf 0x3f3f3f3f
int mapp[maxx][maxx],mach[maxx];//mapp构图,mach存储qi
int in[maxx][20],out[maxx][20];//存储每个机器的输入输出的配建要求
int dep[maxx];
int ans;
int resa[maxx],resb[maxx],resp[maxx];
int st_in(int in[][20],int p,int i)
{
    for(int j=0;j<p;j++)
        if(in[i][j]==1) return 0;
    return 1;
}
int ed_out(int out[][20],int p,int i)
{
    for(int j=0;j<p;j++)
        if(out[i][j]!=1) return 0;
    return 1;
}
int out_in(int out[][20],int in[][20],int i,int j,int p)
{
    for(int k=0;k<p;k++)
        if((out[i][k]+in[j][k])==1) return 0;
    return 1;
}
int BFS(int st,int ed)
{
    memset(dep,-1,sizeof(dep));
    dep[st]=0;
    queue<int >q;
    q.push(st);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<=ed;i++)
        {
            if(mapp[u][i]>0&&dep[i]==-1)
            {
                dep[i]=dep[u]+1;
                q.push(i);
            }
        }
    }return dep[ed]!=-1;
}
int dfs(int u,int ed,int low)
{
    int f;
    if(u==ed) return low;
    for(int i=0;i<=ed;i++)
    {
        if(mapp[u][i]>0&&dep[i]==dep[u]+1&&(f=dfs(i,ed,min(low,mapp[u][i]))))
        {
            mapp[u][i]-=f;
            mapp[i][u]+=f;
            return f;
        }
    }return 0;
}
void Dinic(int st,int ed)
{
    ans=0;
    int f=0;
    while(BFS(st,ed))
    {
        while(f=dfs(st,ed,inf))
        {
            ans+=f;
        }
    }
}
int main()
{
    int p,n,st,ed,cnt;
    while(~scanf("%d%d",&p,&n))
    {
        memset(mach,0,sizeof(mach));
        memset(mapp,0,sizeof(mapp));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        st=0;//超级源点
        ed=2*n+1;//超级汇点
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&mach[i]);
            mapp[i][i+n]=mach[i];
            /*
            拆点,每个机器拆成输入点i和输出点i+n
            输入点和输出点连边,权值为这台机器的qi
            */
            for(int j=0;j<p;j++)
                scanf("%d",&in[i][j]);
            for(int j=0;j<p;j++)
                scanf("%d",&out[i][j]);
            if(st_in(in,p,i)) mapp[st][i]=inf;
            //如果机器的输入中没有1存在,则与源点相连
            if(ed_out(out,p,i))
                mapp[i+n][ed]=inf;
            //如果机器的输出中只有1存在,则与汇点相连
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {/*如果一台机器的输出满足另一台机器的输入,则两台机器连边,边权inf*/
                if(out_in(out,in,i,j,p))
                    mapp[i+n][j]=inf;
            }
        }
        cnt=0;
        Dinic(st,ed);//用Dinic求最大流
        printf("%d ",ans);
        for(int i=1;i<=n;i++)
        {//在除了源点和汇点所在的边内找机器连接
            for(int j=1;j<=n;j++)
            {
                if(i!=j&&mapp[j][i+n]>0)//大于零的反向边就是我们要找的路径
                {
                    resa[cnt]=i;
                    resb[cnt]=j;
                    resp[cnt]=mapp[j][i+n];
                    cnt++;
                }
            }
        }
        printf("%d\n",cnt);
        for(int i=0;i<cnt;i++)
        {
            printf("%d %d %d\n",resa[i],resb[i],resp[i]);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值