POJ3436 ACM Computer Factory(多源点网络流,拆点,EK算法)

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

Hint

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

思路

先说题意:

现在要制造一批电脑,有N个工厂,每个电脑有P个部件,每一个工厂都对进入本工厂的机器有要求,分别用0,1,2表示:

  • 0表示不需要这个部件
  • 1表示需要这个部件
  • 2表示要不要这个部件都可以

经过这个工厂的处理之后,进入工厂的机器,就会变成这个工厂处理之后的状态,还是用0,1表示。
问重新安排生产线之后,最大能组装出多少台电脑,还要输出工厂的连接信息,A B W,从A工厂到B工厂运送W台电脑.

也许你看到这里还是很懵逼,不要着急,我说一下输入输出格式

输入:
先输入P,N代表每台电脑有P个部件,有N台电脑组要组装
接下来有N行,分别描述这N个工厂的信息,对于每一行信息,首先是当前工厂最多能处理的电脑的数目,然后是p个数,描述了该工厂对进入该工厂的机器的要求,然后又是P个数,描述了经过该工厂处理后零件的状态
输出:
首先输出两个数,代表最多能组装出多少台电脑,然后是k行,每一行用三个数描述输出工厂的连接信息,A B W,从A工厂到B工厂运送W台电脑.

然后通过第一个样例来说一下解题思路:

3 4 (代表每个电脑有3部分,现在有4个电脑加工工厂)
15  0 0 0  0 1 0(第一个加工工厂的容量是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(同上)

那么现在我们知道题意了,我们的思路是什么呢。

求的是最多能生产出多少台机器,也就是说,求整个网络中的最大流,那么就要考虑建图的问题了。

我们建图的时候利用一个办法拆点,把每一个工厂拆成两部分,他们的权值为该工厂的容量,那么当货物流经该工厂的时候,就一定要经过这条边,也就是说,通过拆点来限制进入该工厂的流量。

然后需要建立一个超级源点超级汇点,当一个工厂处理电脑的条件是0 0 0的时候,就让超级源点和这个工厂建一条边,权值为inf,当一个工厂处理电脑以后的输出条件是1 1 1的时候,证明一个电脑已经生产完毕,那么就和超级汇点建立一条边。

然后我们再遍历所有的工厂,找到一个工厂的输出满足另一个工厂的输入,再建边,然后用EK算法求出最大流就可以了!

继续用第一个样例解释,最后画出来的网络如下图
这里写图片描述

把第一个工厂拆成了1,2两个点,把第二个工厂拆成了2,3两个点,以此类推接下来的点。0和9为超级源点和超级汇点。

经过求最大流以后的实流网络如图:
这里写图片描述

从实流网络中可以看出,我们需要:

  • 对第1个工厂和第3个工厂建边
  • 对第2个工厂和第3个工厂建边

所以输出为:

25 2
1 3 15
2 3 10

到了这里这个题就结束了,主要是建图的过程,图建好了,这个题就解决了,如果有什么问题,请留言。

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int N=100+20;
int g[N][N],pre[N];
bool vis[N];
int st,ed,p;
bool bfs(int s,int t)
{
    mem(pre,-1);
    mem(vis,false);
    queue<int>q;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=st; i<=ed; i++)
        {
            if(!vis[i]&&g[now][i]>0)
            {
                vis[i]=true;
                pre[i]=now;
                if(i==t)
                    return true;
                q.push(i);
            }
        }
    }
    return false;
}
int EK(int s,int t)
{
    int v,w,d,maxflow=0;
    while(bfs(s,t))
    {
        v=t;
        d=inf;
        while(v!=s)
        {
            w=pre[v];
            d=min(d,g[w][v]);
            v=w;
        }
        maxflow+=d;
        v=t;
        while(v!=s)
        {
            w=pre[v];
            g[w][v]-=d;
            g[v][w]+=d;
            v=w;
        }
    }
    return maxflow;
}

int cp[N][N];//备份图
int in[N][20];//输入信息
int line[N][4];//输出结果

int main()
{
    int p,n;
    while(~scanf("%d%d",&p,&n))
    {
        mem(g,0);
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<2*p+1; j++)
                scanf("%d",&in[i][j]);
        }
        for(int i=1; i<=n; i++) //拆点
            g[2*i-1][2*i]=in[i][0];
        st=0;//源点
        ed=2*n+1;//汇点
        for(int i=1; i<=n; i++)
        {
            bool flag_s=true;
            bool flag_t=true;
            for(int j=0; j<p; j++)
            {
                if(in[i][j+1]==1) flag_s=false;//输入必须全是0
                if(in[i][j+p+1]==0) flag_t=false;//输出必须全是1
            }
            if(flag_s) g[0][2*i-1]=inf;//超级源点和一个工厂容量无限大
            if(flag_t) g[2*i][ed]=inf;//从工厂刀汇点容量无限大
            for(int j=1; j<=n; j++)
            {
                if(i!=j)//枚举不相同的点
                {
                    bool flag=true;
                    for(int k=0; k<p; k++)//枚举部件
                    {
                        //这个点的输出和另一个点的输入不一样的时候,不能建立边
                        if((in[i][k+p+1]==0&&in[j][k+1]==1)||(in[i][k+p+1]==1&&in[j][k+1]==0))
                        {
                            flag=false;
                            break;
                        }
                    }
                    if(flag)//一样的时候建边,权值为这两个工厂所能处理的量的最小值
                        g[2*i][2*j-1]=min(in[i][0],in[j][0]);
                }
            }
        }
        memcpy(cp,g,sizeof(g));
        printf("%d ",EK(st,ed));
        int tot=0;//记录要建立的边的数量
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(g[2*i][2*j-1]<cp[2*i][2*j-1])
                {
                    line[tot][0]=i;
                    line[tot][1]=j;
                    line[tot][2]=cp[2*i][2*j-1]-g[2*i][2*j-1];
                    tot++;
                }
            }
        }
        printf("%d\n",tot);
        for(int i=0; i<tot; i++)
            printf("%d %d %d\n",line[i][0],line[i][1],line[i][2]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值