POJ 3436 ACM Computer Factor (最大流)

本文介绍了一种通过最大流算法解决ACM计算机工厂生产线优化的问题。该问题涉及到如何合理安排不同机器之间的连接以提高整体生产效率。通过构建图模型并运用最大流算法找到最优方案。

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,1Si,2...Si,PDi,1Di,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.

题目大意:一个计算机由p部分构成,有 n 台生产机器,有不同的输入输出规格以及输出速度,每台机器每台机器描述为Qi Si...Sp Di...Dp,Si表示输入规格即输入的产品需满足的·条件,0——不能有这部分,1——必须有这部分,2——可有可无,Di表示输出的规格,0——有这部分,1——没有这部分。

这个题难在题意难懂,搞清楚题意其实就可以建图了,最大流裸题。输入规格为(0,0,0)的或者为(0,0,2)的机器为起点,输出规格为(1,1,1)的为终点,机器之间按输入输出的规格连接(边容量为无穷),每个机器拆为两个点,以容量为其输出速度的边连接。源点 向所有 起点 建容量为无穷的边,所有 终点 向 汇点 建容量为无穷的边。最大流就是答案。输出的边是机器之间流量大于零的正向边,一次DFS就可以求得。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef struct node
{
    int from,to,cap,flow;
    node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}
}node;
const int INF = 1<<30;
const int maxn = 200 + 5;
int n,s,t,p;
vector<node> edge;
vector<int> G[maxn];
int ans1[3000],ans2[3000],ans3[3000];
int cur[maxn],di[maxn];
bool vis[maxn];
int cal;
int S[100][30],D[100][30];
void addedge(int from,int to,int cap,int flow)
{
    edge.push_back(node(from,to,cap,0));
    edge.push_back(node(to,from,0,0));
    int m = edge.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
bool BFS()
{
    int x;
    memset(vis,false,sizeof(vis));
    queue<int> q;
    q.push(s);
    vis[s] = true;
    di[s] = 0;
    while(!q.empty())
    {
        x = q.front();
        q.pop();
        for(int i = 0;i < G[x].size(); ++i)
        {
            node &e = edge[G[x][i]];
            if(e.cap > e.flow && !vis[e.to])
            {
                di[e.to] = di[x] + 1;
                vis[e.to] = true;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int x,int a)
{
    int flow = 0,f;
    if(x == t || a == 0) return a;
    for(int &i = cur[x];i< G[x].size(); ++i)
    {
        node &e = edge[G[x][i]];
        if(di[e.to] == di[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0)
        {
            e.flow += f;
            edge[G[x][i]^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int maxflow()
{
    int flow = 0;
    while(BFS())
    {
        memset(cur,0,sizeof(cur));
        flow += DFS(s,INF);
    }
    return flow;
}
bool judge(int x,int y)
{
    for(int i = 0;i < p; ++i)
    {
        if(D[x][i] != S[y][i] && S[y][i] != 2) return false;
    }
    return true;
}
bool ok(int x,int y)
{
    return (x % 100) != (y % 100) && x != s && x != t && y != s && y != t;
}

void solve(int x)
{
    vis[x] = true;
    int len = G[x].size();
    for(int i = 0;i < len; ++i)
    {
        node e = edge[G[x][i]];
        int u = e.to;
        if(ok(x,u) && e.flow > 0)
        {
            ans1[cal] = x % 100;
            ans2[cal] = u % 100;
            ans3[cal] = e.flow;
            ++cal;
        }
        if(!vis[u] && e.flow > 0) solve(u);
    }
}
int main()
{
    int x,y;
    bool ter[50 + 5];
    scanf("%d %d",&p,&n);
    s = 0,t = 200;
    for(int i = 1;i <= n; ++i)
    {
        int cost;
        scanf("%d",&cost);
        bool r1 = true;
        for(int j = 0;j < p; ++j)
        {
            scanf("%d",&S[i][j]);
            if(S[i][j] == 1) r1 = false;
        }
        bool r2 = true;
        for(int j = 0;j < p; ++j)
        {
            scanf("%d",&D[i][j]);
            if(!D[i][j]) r2 = false;
        }
        ter[i] = r2;
        addedge(i,i + 100,cost,0);
        if(r1) addedge(s,i,INF,0);
        if(r2) addedge(i + 100,t,INF,0);
    }
    for(int i = 1;i <= n; ++i)
    {
        for(int j = 1;j <= n; ++j)
        {
            if(i == j) continue;
            if(judge(i,j)) addedge(i + 100,j,INF,0);
        }
    }
    cout<< maxflow();
    memset(vis,false,sizeof(vis));
    solve(s);
    cout << " " << cal << endl;
    for(int i = 0;i < cal; ++i)
    {
        cout << ans1[i] << " " << ans2[i]  << " "  << ans3[i]  << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值