POJ3436:ACM Computer Factory(最大流)

ACM Computer Factory

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9963   Accepted: 3738   Special Judge

题目链接:http://poj.org/problem?id=3436

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 30 0

Hint

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

题意:

这个题意难度有点大呀TnT。

简单说就是给出n个机器,每个机器都有一个pi代表生产效率,然后后面有2*p个数,前p个代表输入规范,后p个代表输出规范。这里输入输出规范的意思就是这台机器可以把输入规范转化为输出规范。

输入规范里面0就是这里没有零件,1就是这里必须有零件,2就是这里可有可不有;输出规范里面就只有0,1,意义同上。

现在问最多可以生产出多少台电脑(假设机器的配合不消耗时间= =),只有输出规范全为1的机器可以生产电脑。

题解:

这题用最大流来做。首先建立一个超级源点和超级汇点,超级源点连上输入规范全为0,或有0也有2的机器,因为这些机器可以“无中生有”,边权为无穷大。

然后所有输出规范为1的机器就连向超级汇点,毕竟此时可以生成电脑,边权也为无穷大。

然后建立可以相互可达的机器之间的边,这里由于每个点有个生产效率的权值,所以我们考虑把点拆开为一条权值为其生产效率的有向边,拆成的两个点分别代表入读点和出度点。这样可以限定一条生产线上的生产效率。

然后直接跑最大流就好了~

最后统计结果的时候就随便统计一下就好了...毕竟special judge。如果一个出度点到一个入读点的边上面有流量,就代表了两个点之间有合作关系,就输出这两个点。

代码如下:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#define INF 99999999
using namespace std;

const int N = 150;
int P,n,tot;
int p[N],m[N][N],head[N],cur[N],d[N];

struct Edge{
    int u,v,c,flow,next;
}e[N<<1];
void adde(int u,int v,int w,int f){
    e[tot].v=v;e[tot].u=u;e[tot].c=w;e[tot].flow=f;
    e[tot].next=head[u];head[u]=tot++;
}
bool bfs(int s,int t){
    for(int i=0;i<=2*n+2;i++) d[i]=0;d[s]=1;
    queue <int > q;q.push(s);
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(!d[v] && e[i].c>e[i].flow){
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[t]!=0;
}
int dfs(int s,int a){
    if(s==2*n+1 || a==0) return a;
    int flow = 0;
    for(int &i=cur[s];i!=-1;i=e[i].next){
        int v=e[i].v,f;
        if(d[v]!=d[s]+1) continue ;
        f=dfs(v,min(a,e[i].c-e[i].flow));
        if(f){
            e[i].flow+=f;
            e[i^1].flow-=f;
            a-=f;
            flow+=f;
            if(a==0) break;
        }
    }
    if(!flow) d[s]=-1;
    return flow;
}
int main(){
    scanf("%d%d",&P,&n);
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++){
        scanf("%d",&p[i]);
        for(int j=1;j<=2*P;j++) scanf("%d",&m[i][j]);
    }

    for(int i=1;i<=n;i++){
        adde(i,i+n,p[i],0);
        adde(i+n,i,0,0);
        int flag1=1,flag2=1;
        for(int j=1;j<=P;j++){
            if(m[i][j]==1) flag1=0;
            if(m[i][j+P]!=1) flag2=0;
        }
        if(flag1) adde(0,i,INF,0),adde(i,0,0,0);
        if(flag2) adde(i+n,2*n+1,INF,0),adde(2*n+1,i+n,0,0);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j) continue ;
            bool ok = true ;
            for(int k=P+1;k<=P*2;k++){
                int now = k-P;
                if(m[j][now]==2) continue ;
                if(m[i][k]!=m[j][now]) ok=false;
            }
            if(ok){
                adde(i+n,j,INF,0);
                adde(j,i+n,0,0);
            }
        }
    }

    int max_flow = 0;
    while(bfs(0,2*n+1)){
        for(int i=0;i<=2*n+1;i++) cur[i]=head[i];
        max_flow+=dfs(0,INF);
    }
    printf("%d ",max_flow);
    int tot=0;
    vector <pair<int,int> > ans[N];
    for(int i=1+n;i<=2*n;i++){
        for(int j=head[i];j!=-1;j=e[j].next){
            int v=e[j].v;
            if(v!=2*n+1 && v!=0 && e[j].flow && v!=i-n) ans[i-n].push_back(make_pair(v,e[j].flow)),tot++;
        }
    }
    printf("%d\n",tot);
    for(int i=1;i<=n;i++)
        for(int j=0;j<ans[i].size();j++){
            printf("%d %d %d\n",i,ans[i][j].first,ans[i][j].second);
        }
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/10061764.html

时间: 2024-08-03 15:33:47

POJ3436:ACM Computer Factory(最大流)的相关文章

poj3436 ACM Computer Factory, 最大流,输出路径

POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi. 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移除某些部件).求电脑公司的单位时间最大产量,以及哪些机器有协作关系,即一台机器把它的产品交给哪些机器加工. Sample input 3 4 15  0 0 0  0 1 0 10  0 0 0  0 1 1 30  0 1 2  1 1 1 3

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 i

POJ3436 ACM Computer Factory 【最大流】

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5412   Accepted: 1863   Special Judge Description As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. Th

POJ-3436 ACM Computer Factory(网络流EK)

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

Poj 3436 ACM Computer Factory (最大流)

题目链接: Poj 3436 ACM Computer Factory 题目描述: n个工厂,每个工厂能把电脑s态转化为d态,每个电脑有p个部件,问整个工厂系统在每个小时内最多能加工多少台电脑? 解题思路: 因为需要输出流水线要经过的工厂路径,如果要用电脑状态当做节点的话,就GG了.所以建图的时候要把工厂当做节点.对于节点i,能生产si电脑的节点可以进入节点i,能转化ei电脑的节点可以由i节点进入.要注意对于每一个节点要进行拆点,防止流量发生错误. 1 #include <queue> 2 #

poj 3436 ACM Computer Factory 最大流拆点+输出路径

题目链接: poj3436 题意: 每台ACM 计算机包含P 个部件,当所有这些部件都准备齐全后,计算机就可以组装了,组装好以后就可以交给竞赛队伍使用了.计算机的生产过程是全自动的,通过N 台不同的机器来完成.每台机器从一台半成品计算机中去掉一些部件,并加入一些新的部件(去除一些部件在有的时候是必须的,因为计算机的部件不能以任意的顺序组装).每台机器用它的性能(每小时组装多少台计算机).输入/输出规格来描述. 输入规格描述了机器在组装计算机时哪些部件必须准备好了.输入规格是由P 个整数组成,每个

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 particip

POJ 3436 ACM Computer Factory (最大流 + 输出路径)

POJ 3436 ACM Computer Factory 链接:http://poj.org/problem?id=3436 题意:每台电脑有P部分,可以通过不同的机器来进行加工.有N台机器,每台机器用2 P +1 个整数来描述:Qi  Si,1  Si,2 ...  Si,p  Di,1  Di,2. ..  Di,p,其中Qi 指定了机器的性能,表示每小时加工的电脑数量.Si,j 为第j 部分的输入规格,0表示该部分不能被加工过,1表示该部分必须被加工过,2表示都可以.Di,k 为第k 部

解题报告 之 POJ3463 ACM Computer Factory

解题报告 之 POJ3463 ACM Computer Factory 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 A