poj 3436 ACM Computer Factory(最大流)

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

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


题意:此题是最大流问题。题目意思比较难懂。 看得比较纠结。 就是说有N台组装电脑的机器。电脑的组成部分共有P部分。 每台机器有P个输入输出规格。还有一个容量Q; 其中输入规格有三种情况:0,1,2  0:该部分不能存在  1:该部分必须保留  2:该部分可有可无  输出规格有2种情况:0,1  0:该部分不存在  1:该部分存在  要求的是生产电脑最大的台数,就是求网络中的最大流


解析:这相当于是生产线。需要自己去建图。  但是考虑到每台机器都有容量,所以把一台机器分成两个点,中间建一条容量的边。  同时如果一台机器的输出符合另一台机器的输入,则建一条容量无穷大的边。     同时要增加源点和汇点。输入没有1的连接源点,输出全部是1的连接汇点。这里运用了EK算法,还有一个、Dinic算法也可以解决这类问题,


借鉴博客: http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649346.html

代码:


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define N 110
using namespace std;
const int INF = 0x3f3f3f3f;

struct edge
{
    int w, in[11], out[11];
}G[N];

int p, n, mp[N][N], mp2[N][N];
int pre[N<<1], used[N<<1];

bool check(int a, int b)
{
    for(int i = 0; i < p; i++)
    {
        if(!(G[a].out[i] == G[b].in[i] || G[b].in[i] == 2)) return false;
    }
    return true;
}

void init()
{
    memset(mp, 0, sizeof(mp));
    for(int i = 0 ; i < p; i++)
    {
        G[0].in[i] = G[0].out[i] = 0;
        G[n+1].in[i] = G[n+1].out[i] = 1;
    }
    G[0].w = G[n+1].w = INF;
    n += 2;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(i == j) mp[j + n][i] = G[i].w;
            else if(check(i, j)) mp[i][j + n] = G[i].w;
        }
    }
    n += n;
    memcpy(mp2, mp, sizeof(mp));
}

int EK()
{
    int ans = 0;
    while(1)
    {
        memset(pre, -1, sizeof(pre));
        memset(used, 0, sizeof(used));
        queue<int> q;
        q.push(n/2);
        used[n/2] = 1;
        while(!q.empty())
        {
            int u = q.front(); q.pop();
            for(int i = 0; i < n; i++)
            {
                if(!used[i] && mp[u][i])
                {
                    pre[i] = u;
                    used[i] = 1;
                    q.push(i);
                }
            }
        }
        if(pre[n/2 - 1] == -1) break;
        int m = INF;
        for(int i = n/2-1; pre[i] != -1; i = pre[i])
        {
            m = min(m, mp[pre[i]][i]);
        }
        for(int i = n/2-1; pre[i] != -1; i = pre[i])
        {
            mp[pre[i]][i] -= m;
            mp[i][pre[i]] += m;
        }
        ans += m;
    }
    return ans;
}


int main()
{
    int num = 0, a[N], b[N], c[N];
    scanf("%d%d", &p, &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &G[i].w);
        for(int j = 0; j < p; j++) scanf("%d", &G[i].in[j]);
        for(int j = 0; j < p; j++) scanf("%d", &G[i].out[j]);
    }
    init();
    int ans = EK();
    n = n / 2;
    int nn = n - 2;
    for(int i = 1; i <= nn; i++)
    {
        for(int j = 1; j <= nn; j++)
        {
            if(i == j) continue;
            if(mp2[i][j+n] > mp[i][j+n])
            {
                a[num] = i; b[num] = j;
                c[num++] = mp2[i][j+n] - mp[i][j+n];
            }
        }
    }

    printf("%d %d\n", ans, num);
    for(int i = 0; i < num; i++) printf("%d %d %d\n", a[i], b[i], c[i]);

    return 0;

}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值