poj 3436 ACM Computer Factory 【图论-网络流-最大流-EK-输出路径】

                            ACM Computer Factory
                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 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 3
0 0

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

==============================================================================================================
分割线内的内容转自:http://www.cnblogs.com/wangyiming/p/6357709.html

题目大意:
电脑公司生产电脑有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 0 2 1 1 1 1

Sample output
25 2
1 3 15
2 3 10

输入:电脑由3个部件组成,共有4台机器,1号机器产量15, 能给空电脑加上2号部件,2号 机器能给空电脑加上2号部件和3号部件, 3号机器能把有1个2号部件和3号部件有无均可的电脑变成成品(每种部件各有一个)
输出:单位时间最大产量25,有两台机器有协作关系,
1号机器单位时间内要将15个电脑给3号机器加工
2号机器单位时间内要将10个电脑给3号机器加工

思路:
网络流模型:
(1) 添加一个原点S,S提供最初的原料 00000…
(2) 添加一个汇点T, T接受最终的产品 11111…
(3) 将每个机器拆成两个点: 编号为i的接收节点,和编号为i+n的产出节点(n是机器数目),前者用于接收原料,后者用于提供加工后的半成品或成品。这两个点之间要连一条边,容量为单位时间产量Qi
(4) S 连边到所有接收 0000”或 “若干个0及若干个2” 的机器,容量为无穷大
(5) 产出节点连边到能接受其产品的接收节点,容量无穷大
(6) 能产出成品的节点,连边到T,容量无穷大。
(7) 求S到T的最大流

==============================================================================================================

AC代码:

# include <iostream>
# include <cstdio>
# include <cstring>

using namespace std;

# define MAXN 1005
# define MAXP 15
# define INF 1e9+10

struct LINE
{
    int w;          //记录机器i单位时间内加工的数量
    int in[MAXP];   //记录机器i未加工前的情况
    int out[MAXP];  //记录机器i加工后的情况
}L[MAXN];

struct EDGE //邻接表形式存放数据
{
    int v;
    int init_w;     //记录边的容量
    int w;          //记录边的流量
    int re;         //记录反向边的编号
    int next;
}edge[MAXN];

int p, n;           //题目中P和N
int tot;
int ehead[MAXN];
int pre[MAXN];      //记录前驱节点
int vis[MAXN];      //用于判断该节点是否已经访问
int que[MAXN];      //数组模拟队列

int min(int a, int b)
{
    return a > b ? b : a;
}

void Addedge(int u, int v, int w)
{
    edge[tot].v = v;
    edge[tot].init_w = w;
    edge[tot].w = w;
    edge[tot].re = tot + 1;
    edge[tot].next = ehead[u];
    ehead[u] = tot++;

    //反向连接,初始化残余流量为0,初始容量为0
    edge[tot].v = u;
    edge[tot].init_w = 0;
    edge[tot].w = 0;
    edge[tot].re = tot - 1;
    edge[tot].next = ehead[v];
    ehead[v] = tot++;
}

bool EK()
{
    int head = 1;
    int tail = 1;
    memset(vis, 0, sizeof(vis));  //每次寻找增广路,需要把已经访问的清零,即变为未访问
    que[tail++] = 0; //把源点加入队列
    vis[0] = 1;     //标记源点为已访问
    while (tail > head) //当队列非空时
    {
        int u = que[head++];
        for (int i = ehead[u]; i != 0; i = edge[i].next)
        {
            int v = edge[i].v;

            if (!vis[v] && edge[i].w) //如果v节点未被访问并且该边为连通边
            {
                pre[v] = i; //记录v的前驱节点为i
                if (v == 2 * n + 1) //到达汇点,即找到一条增广路
                {
                    return true;
                }
                que[tail++] = v;
                vis[v] = 1;
            }
        }
    }
    return false; //没有找到增广路,即找到了最大流量
}

void Readdata()
{
    int i, j, k, flag;
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &L[i].w);
        flag = 1;
        for (j = 0; j < p; j++)
        {
            scanf("%d", &L[i].in[j]);
            if (1 == L[i].in[j])
            {
                flag = 0;
            }
        }
        if (flag)
        {
            Addedge(0, i, INF); //把未加工前的电脑未安装任何必须部件的机器编号与虚拟源点0相连
        }

        flag = 1;
        for (j = 0; j < p; j++)
        {
            scanf("%d", &L[i].out[j]);

            if (1 != L[i].out[j])
            {
                flag = 0;
            }
        }
        if (flag)
        {
            Addedge(n + i, 2 * n + 1, INF); //把加工后为完整电脑的机器编号与虚拟汇点 2 * n + 1 相连
        }
    }

    for (i = 1; i <= n; i++)
    {
        Addedge(i, n + i, L[i].w);
        for (j = 1; j <= n; j++)
        {
            if (i == j)
            {
                continue;
            }
            flag = 1;
            for (k = 0; k < p; k++)
            {
                if (L[j].in[k] != 2 && L[j].in[k] != L[i].out[k])
                {
                    flag = 0;
                }
            }
            if (flag)
            {
                Addedge(n + i, j, INF);
            }
        }
    }
}

void Solve()
{
    int u, p;
    int maxflow = 0;
    while (EK())
    {
        int minflow = INF;
        //利用前驱寻找路径
        for (u = 2 * n + 1; u != 0; u = edge[edge[p].re].v)
        {
            p = pre[u];
            minflow = min(minflow, edge[p].w);
        }
        for (u = 2 * n + 1; u != 0; u = edge[edge[p].re].v)
        {
            p = pre[u];
            edge[p].w -= minflow;
            edge[edge[p].re].w += minflow;
        }
        maxflow += minflow;
    }
    int i;
    int num = 0;
    for (u = n + 1; u < 2 * n + 1; u++)
    {
        for (i = ehead[u]; i != 0; i = edge[i].next)
        {
            if (edge[i].v > 0 && edge[i].v <= n && edge[i].init_w > edge[i].w)
            {
                num++;  //有多少可行边
            }
        }
    }
    printf("%d %d\n", maxflow, num);
    for (u = n + 1; u < 2 * n + 1; u++)
    {
        for (i = ehead[u]; i != 0; i = edge[i].next)
        {
            if (edge[i].v > 0 && edge[i].v <= n && edge[i].init_w > edge[i].w)
            {
                printf("%d %d %d\n", u - n, edge[i].v, edge[i].init_w - edge[i].w); //一次输出变化的边,以及流经的流量
            }
        }
    }
}

int main(void)
{
    memset(ehead, 0, sizeof(ehead));
    tot = 1;
    scanf("%d %d", &p, &n);
    Readdata(); //读取数据
    Solve();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值