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

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.

题意:就是说有N台组装电脑的机器。电脑的组成部分共有P部分。每台机器有P个输入输出规格。还有一个容量Q;其中输入规格有三种情况:0,1,2      0:该部分不能存在      1:该部分必须保留      2:该部分可有可无

输出规格有2种情况:0,1     0:该部分不存在    1:该部分存在     

要求的是生产电脑最大的台数。

思路:这道题让你求生产电脑最大的台数,其实就是让你求最大流,关键还是在与建图。考虑到每台机器都有容量,所以把一台机器分成两个点,中间建一条容量的边。同时如果一台机器的输出符合另一台机器的输入,则建一条容量无穷大的边。同时要增加源点和汇点。输入没有1的连接源点,输出全部是1的连接汇点。

AC代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
const int maxx=110;
const int inf=0x3f3f3f3f;
typedef long long ll;
using namespace std;
int c[maxx][maxx];
int pre[maxx],flow[maxx];
int backup[maxx][maxx];
int in[maxx][20];
int line[maxx][4];
int n,start,endd;
queue<int>q;
int bfs()
{
    int i,t;
    while(!q.empty())
        q.pop();
    memset(pre,-1,sizeof(pre));
    pre[start]=0;
    flow[start]=inf;
    q.push(start);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(t==endd)break;
        for(i=0; i<=n; i++)
        {
            if(i!=start && pre[i]==-1 && c[t][i])
            {
                flow[i]=min(flow[t],c[t][i]);
                q.push(i);
                pre[i]=t;
            }
        }
    }
    if(pre[endd]==-1)
        return -1;
    return flow[endd];
}
int maxflow()
{
    int sumflow=0;
    int ans,u,v;
    while((ans=bfs())!=-1)
    {
        sumflow+=ans;
        u=endd;
        while(u!=start)
        {
            v=pre[u];
            c[v][u]-=ans;
            c[u][v]+=ans;
            u=v;
        }
    }
    return sumflow;
}
int main()
{
    int p;
    int N;
    while(~scanf("%d%d",&p,&N))
    {
        memset(c,0,sizeof(c));
        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++)
        {
            c[2*i-1][2*i]=in[i][0];
        }
        n=2*N+1;
        start=0;
        endd=n;
        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;
                if(in[i][j+1+p]==0)
                    flag_t=false;
            }
            if(flag_s)
                c[0][2*i-1]=inf;
            if(flag_t)
                c[2*i][n]=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)
                    c[2*i][2*j-1]=min(in[i][0],in[j][0]);
                }
        }
        memcpy(backup,c,sizeof(c));
        printf("%d ",maxflow());
        int tol=0;
        for(int i=1; i<=N; i++)
            for(int j=1; j<=N; j++)
            {
                if(c[2*i][2*j-1]<backup[2*i][2*j-1])
                {
                    line[tol][0]=i;
                    line[tol][1]=j;
                    line[tol][2]=backup[2*i][2*j-1]-c[2*i][2*j-1];
                    tol++;
                }
            }
        printf("%d\n",tol);
        for(int i=0; i<tol; i++)
        {
            printf("%d %d %d\n",line[i][0],line[i][1],line[i][2]);
        }
    }
    return 0;
}
发布了204 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43846139/article/details/103321454