poj 3436 ACM Computer Factory (最大流 拆点建图 找路径)@

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 ofP 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 Pnumbers 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,PDi,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.

题意:每台电脑共有p种零件,现在有n台机器,给出n台机器每台需要的一些种类零件当原料(0代表不需要,1代表必须要,2代表可有可无)和输出的产品零件。问怎么安排生产线使生产出来零件可以组装的电脑最多。

思路:如果机器的原材料什么都不需要的话(0,0,0)就可以当源点,如果机器输出的零件种类为(1,1,1)就可以当汇点。这题只有当一台机器的输出格式跟另一台的输入格式一样时才可以相连,即x机器必须需要的部件,y必须产出,y产出的部件x必须需要或者可有可无


#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 2100;
const int inf = 99999999;
int dis[N], lter[N];
struct node
{
    int to, cap, rev, cap2, id;
};
vector<node>p[N];
void add(int u,int v,int w,int id)
{
    node tmp;
    tmp.to=v, tmp.cap=w,tmp.cap2=w,tmp.id=id,tmp.rev=p[v].size();
    p[u].push_back(tmp);
    tmp.to=u, tmp.cap=0,tmp.cap2=0,tmp.id=id,tmp.rev=p[u].size()-1;
    p[v].push_back(tmp);
    return ;
}

int bfs(int s,int t)
{
    memset(dis,-1,sizeof(dis));
    queue<int>q;
    dis[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0; i<p[u].size(); i++)
        {
            int v=p[u][i].to;
            if(dis[v]==-1&&p[u][i].cap>0)
            {
                dis[v]=dis[u]+1;
                q.push(v);
            }
        }
    }
    return dis[t]==-1;
}

int dfs(int s,int t,int flow)
{
    if(s==t) return flow;
    for(int &i=lter[s]; i<p[s].size(); i++)
    {
        int v=p[s][i].to;
        if(dis[v]==dis[s]+1&&p[s][i].cap>0)
        {
            int f=dfs(v,t,min(flow,p[s][i].cap));
            if(f>0)
            {
                p[s][i].cap-=f, p[v][p[s][i].rev].cap+=f;
                return f;
            }
        }
    }
    return 0;
}

int dinic(int s,int e)
{
    int flow=0, f;
    while(1)
    {
        while(bfs(s,e)) return flow;
        memset(lter,0,sizeof(lter));
        while(f=dfs(s,e,inf),f>0)
        {
            flow+=f;
        }
    }
    return flow;
}

struct node1
{
    int in[20], out[20], val;
}q[N];

struct node2
{
    int x,y,z;
}ans[N];
int main()
{
    int n, m, flag;
    while(scanf("%d %d",&n, &m)!=EOF)
    {

        int s=2*m+1, e=2*m+2;
        for(int i=0;i<=e;i++) p[i].clear();
        for(int i=1;i<=m;i++)
        {
            scanf("%d", &q[i].val);
            flag=0;
            for(int j=1;j<=n;j++)
            {
                scanf("%d", &q[i].in[j]);
                if(q[i].in[j]==1) flag=1;
            }
            if(flag==0) add(s,i,inf,i);
            flag=0;
            for(int j=1;j<=n;j++)
            {
                scanf("%d", &q[i].out[j]);
                if(q[i].out[j]==0) flag=1;
            }
            if(flag==0) add(i+m,e,inf,inf);
        }
        for(int i=1;i<=m;i++)
        {
            add(i,i+m,q[i].val,i);
            for(int j=1;j<=m;j++)
            {
                if(j==i) continue;
                flag=0;
                for(int k=1;k<=n;k++)
                {
                    if(q[i].out[k]+q[j].in[k]==1)
                    {
                        flag=1;break;
                    }
                }
                if(flag==0) add(i+m,j,inf,j);
            }
        }
        int flow=dinic(s,e);
        int k=0;
        for(int i=m+1;i<=2*m;i++)
        {
            for(int j=0;j<p[i].size();j++)
            {
                if(p[i][j].id==i||p[i][j].id==inf) continue;
                if(p[i][j].cap2>p[i][j].cap)
                {
                    ans[k].x=i-m, ans[k].y=p[i][j].id, ans[k].z=p[i][j].cap2-p[i][j].cap;
                    k++;
                }
            }
        }
        printf("%d %d\n",flow,k);
        for(int i=0;i<k;i++) printf("%d %d %d\n",ans[i].x, ans[i].y, ans[i].z);
    }
    return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值