首页 > 代码库 > POJ 3436 ACM Computer Factory(网络最大流)

POJ 3436 ACM Computer Factory(网络最大流)

http://poj.org/problem?id=3436

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

Source

Northeastern Europe 2005, Far-Eastern Subregion


题意:

流水线上有N台机器装电脑,电脑有P个部件,每台机器有三个参数,产量,输入规格,输出规格;输入规格中0表示改部件不能有,1表示必须有,2无所谓;输出规格中0表示改部件没有,1表示有。问如何安排流水线(如何建边)使产量最高。

分析:

这是道网络最大流

首先拆点,因为点上有容量限制,在拆出的两点间连一条边,容量为产量;

如果输入规格中没有1,则与超级源点相连,容量为无穷大;

如果输出规格中没有0,则与超级汇点相连,容量为无穷大;

判断和其他机器的输入输出是否匹配,能匹配上则建边,容量为无穷大;

在该图上跑一遍最大流,容量大于零的边集即为方案。


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 210007
#define maxn 107

using namespace std;

int fir[maxn];
int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];
int e_max;
int lv[maxn],q[maxn],iter[maxn];

void add_edge(int _u,int _v,int _w)
{
    int e;
    e=e_max++;
    u[e]=_u;v[e]=_v;cap[e]=_w;nex[e]=fir[u[e]];fir[u[e]]=e;
    e=e_max++;
    u[e]=_v;v[e]=_u;cap[e]=0;nex[e]=fir[u[e]];fir[u[e]]=e;
}

void dinic_bfs(int s)
{
    int f,r;
    memset(lv,-1,sizeof lv);
    lv[s]=0;
    q[f=r=0]=s;
    while (f<=r)
    {
        int x=q[f++];
        for (int e=fir[x];~e;e=nex[e])
        {
            if(cap[e]>flow[e] && lv[v[e]]<0)
            {
                lv[v[e]]=lv[u[e]]+1;
                q[++r]=v[e];
            }
        }
    }
}

int dinic_dfs(int _u,int t,int _f)
{
    if (_u==t)  return _f;
    for (int &e=iter[_u];~e;e=nex[e])
    {
        if (cap[e]>flow[e] && lv[u[e]]<lv[v[e]])
        {
            int _d=dinic_dfs(v[e],t,min(_f,cap[e]-flow[e]));
            if (_d>0)
            {
                flow[e]+=_d;
                flow[e^1]-=_d;
                return _d;
            }
        }
    }

    return 0;
}

int max_flow(int s,int t)
{
    memset(flow,0,sizeof flow);
    int total_flow=0;

    for (;;)
    {
        dinic_bfs(s);
        if (lv[t]<0)    return total_flow;

        memcpy(iter,fir,sizeof fir);

        int _f;
        while ((_f=dinic_dfs(s,t,INF))>0)
            total_flow+=_f;
    }
    return total_flow;
}

struct node
{
    int in[10],out[10];
    int in1,out0;
}a[55];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    int p,n,s,t,_w;

    e_max=0;
    memset(fir,-1,sizeof fir);
    scanf("%d %d",&p,&n);
    s=0;t=n+n+1;

    for (int i=1;i<=n;i++)
    {
        scanf("%d",&_w);
        add_edge(i,i+n,_w);
        a[i].in1=0;
        for (int j=0;j<p;j++)
        {
            scanf("%d",&a[i].in[j]);
            if (a[i].in[j]==1)  a[i].in1++;
        }
        a[i].out0=0;
        for (int j=0;j<p;j++)
        {
            scanf("%d",&a[i].out[j]);
            if (a[i].out[j]==0)  a[i].out0++;
        }

        if (a[i].in1==0)    add_edge(s,i,INF);
        if (a[i].out0==0)    add_edge(i+n,t,INF);

        for (int j=1;j<i;j++)
        {
            if (a[j].in1!=0 && a[i].out0!=0)
            {
                bool flag=true;
                for (int k=0;flag && k<p;k++)
                {
                    if (a[i].out[k]+a[j].in[k]==1)
                        flag=false;
                }
                if (flag)
                    add_edge(i+n,j,INF);
            }
            if (a[i].in1!=0 && a[j].out0!=0)
            {
                bool flag=true;
                for (int k=0;flag && k<p;k++)
                {
                    if (a[j].out[k]+a[i].in[k]==1)
                        flag=false;
                }
                if (flag)
                    add_edge(j+n,i,INF);
            }
        }
    }

    printf("%d ",max_flow(s,t));

    int m=0;
    for (int e=0;e<e_max;e++)
    {
        if (flow[e]<=0 || u[e]==s || v[e]==t || v[e]-u[e]==n) continue;
        u[m]=u[e]>n?u[e]-n:u[e];
        v[m]=v[e];
        flow[m]=flow[e];
        m++;
    }

    printf("%d\n",m);
    for (int e=0;e<m;e++)
        printf("%d %d %d\n",u[e],v[e],flow[e]);

    return 0;
}