POJ 3436ACM Computer Factory 最大流+路径输出 dinic模板

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,1Si,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.

题意:

为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产线来生产这样的电脑,随着比赛规模的越来越大,生产线的生产能力不能满足需要,所以说ACM董事会想要重新建造一条生产线。


        生产线是全自动化的,所以需要机器来组成生产线,给定有多少中种机器,标准ACM用电脑有多少部份,每种机器将什么样的ACM电脑半成品处理成什么样的电脑半成品(对于输入的电脑半成品,每部分有0,1,2三种状态:代表着 0、这部分必须没有我才能处理,1、这部分必须有我才能处理,2、这部分有没有我都能处理。对于输出的电脑半成品有0,1两种状态:代表着0,处理完后的电脑半成品里没有这部分,1、处理完的电脑半成品有这部分),每一个机器每小时可以处理Q个半成品(输入数据中的Qi)。


        求组装好的成产线的最大工作效率(每小时最多生成多少成品,成品的定义就是所有部分的状态都是“1”),和每两个机器之间的流量。

解题思路:

把所有输入状态为不含1的机器连接在源点0上,所有输出状态不含0的机器连接在汇点n+1上,流量就是这些点的产量。

然后把1-n这些节点依次与其他结点对比一下,看输出口和输入口能否对上,能对上就连起来。然后图就构成了~~

套dinic模板即可。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=60;
const int M=60*60*2;
int n,p;
int s,t,e_max,fir[N],dis[N],q[N];
struct Node
{
        int c;
        int in[15],ou[15];
}a[N];
struct edge
{
    int u,v,w,nex;
}e[M];
void add_edge(int u,int v,int w)
{
    e[e_max].u=u;e[e_max].v=v;e[e_max].w=w;e[e_max].nex=fir[u];
    fir[u]=e_max++;
    e[e_max].u=v;e[e_max].v=u;e[e_max].w=0;e[e_max].nex=fir[v];
    fir[v]=e_max++;
}
int bfs()
{
    int i,x,v,tail=0,head=0;
    memset(dis,0,sizeof(dis));
    dis[s]=1;
    q[tail++]=s;
    while(head<tail)
    {
        x=q[head++];
        for(i=fir[x];i!=-1;i=e[i].nex)
            if(e[i].w&&dis[v=e[i].v]==0)
            {
                dis[v]=dis[x]+1;
                if(v==t)
                    return 1;
                q[tail++]=v;
            }
    }
    return 0;
}
int dfs(int s,int limit)
{
        if(s==t)
                return limit;
        int i,v,tmp,cost=0;
        for(i=fir[s];i!=-1;i=e[i].nex)
        if(e[i].w&&dis[s]==dis[v=e[i].v]-1)
        {
            tmp=dfs(v,min(limit-cost,e[i].w));
            if(tmp>0)
            {
                e[i].w-=tmp;
                e[i^1].w+=tmp;
                cost+=tmp;
                if(limit==cost)
                    break;
            }
            else dis[v]=-1;
        }
    return cost;
}
int Dinic()
{
    int ans=0;
    while(bfs())
        ans+=dfs(s,INF);
    return ans;
}

void init()
{
        s=0;
        t=n+1;
        memset(fir,-1,sizeof(fir));
        e_max=0;
}
void read()
{
        init();
        for(int i=1;i<=n;i++)
        {
                cin>>a[i].c;
                int flag1=0,flag2=0;
                for(int j=1;j<=p;j++)
                {
                        cin>>a[i].in[j];
                        if(a[i].in[j]==1)
                                flag1=1;
                }
                for(int j=1;j<=p;j++)
                {
                        cin>>a[i].ou[j];
                        if(a[i].ou[j]==0)
                                flag2=1;
                }
                if(flag1==0)
                        add_edge(0,i,a[i].c);
                if(flag2==0)
                        add_edge(i,n+1,a[i].c);
        }
}
void makegr()
{
        for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                {
                        if(i==j)
                                continue;
                        int flag=0;
                        for(int k=1;k<=p;k++)
                        {
                                if(a[i].ou[k]!=a[j].in[k]&&a[j].in[k]!=2)
                                        flag=1;
                        }
                        if(flag==0)
                                add_edge(i,j,a[i].c);
                }
}
int main()
{
        while(cin>>p>>n)
        {
                read();
                makegr();
                cout<<Dinic()<<' ';
                int w=0;
                for(int i=0;i<e_max;i+=2)
                {
                        if(e[i].u==0||e[i].v==n+1)
                                continue;
                        if(e[i].w!=a[e[i].u].c)
                                ++w;
                }
                cout<<w<<endl;
                for(int i=0;i<e_max;i+=2)
                {
                        if(e[i].u==0||e[i].v==n+1)
                                continue;
                        if(e[i].w!=a[e[i].u].c)
                                cout<<e[i].u<<' '<<e[i].v<<' '<<a[e[i].u].c-e[i].w<<endl;
                }
        }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值