poj 3436 ACM Computer Factory(最大流Dinic拆点)

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

题意:

一个电脑的生产线 各台机器完成电脑零件的组装 每台机器  组装电脑时对电脑已经安装的零件有要求

每台机器的性能不同  同一时间内安装的电脑数量不同  现在求最大的生产效率 

以及生产线上使用的机器的顺序


网络流  建图

如果该机器所需要的电脑零件初始状态没有1(注意为2的情况 也是符合该条件的) 该点就与源点建边

如果该机器组装电脑完毕零件状态都为1  该点就与汇点建边

拆点 i与i+n之间建边 权值为q[i]

如果该机器安装完毕的电脑零件状态满足另一机器安装电脑零件的初始状态 这两点之间建边 

然后就是判断 最大流路径中的点

我是通过边中flow的值为负的情况进行判断的  当然这条边中的初始点终点是有要求的

这两个点中 没有一个是源点 没有一个是汇点 这两个点不是同一机器的两个拆点

(网络的值为负 说明这条边在增光路上)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define eps 1e-8
#define op operator
#define MOD  10009
#define MAXN  41000
#define INF   99999999
#define MEM(a,x)    memset(a,x,sizeof a)
#define ll long long

using namespace std;

int q[60];
int in[60][20],out[60][20];

struct Edge
{
    int from,to,cap,flow;
    bool operator <(const Edge e) const
    {
        if(e.from!=from)  return from<e.from;
        else return to<e.to;
    }
    Edge() {}
    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};

struct Dinic
{
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];//BFS使用
    int d[MAXN];   //从起点到i的距离
    int cur[MAXN]; //当前弧下标
    int n,m,s,t,maxflow;   //节点数 边数(包括反向弧) 源点编号和弧点编号

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

//    void clearflow()
//    {
//        for(int i=0;i<edges.size();i++)
//            edges[i].flow=0;
//    }

    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int a)
    {
        if(u==t||a==0)  return a;
        int sz=G[u].size();
        int flow=0,f;
        for(int &i=cur[u];i<sz;i++)
        {
            Edge &e=edges[G[u][i]];
            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[u][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)  break;
            }
        }
        return flow;
    }

    int Maxflow(int s,int t)
    {
        this->s=s; this->t=t;
        int flow=0;
        while(bfs())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }


}Dic;

vector<int> cut;
vector<Edge> ans;

int main()
{
//    freopen("ceshi.txt","r",stdin);
    int p,n;
    while(scanf("%d%d",&p,&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&q[i]);
            for(int j=1;j<=p;j++)
                scanf("%d",&in[i][j]);
            for(int j=1;j<=p;j++)
                scanf("%d",&out[i][j]);
        }
        int s=0,t=2*n+1;
        Dic.init(t);
        for(int i=1;i<=n;i++)
        {
            int flag=1;
            for(int j=1;j<=p;j++)
            {
                if(in[i][j]==1)
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
                Dic.addedge(s,i,INF);
        }
        for(int i=1;i<=n;i++)
        {
            int flag=1;
            for(int j=1;j<=p;j++)
            {
                if(out[i][j]==0)
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
                Dic.addedge(i+n,t,INF);
        }
        for(int i=1;i<=n;i++)
            Dic.addedge(i,i+n,q[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j)
                    continue;
                int flag=1;
                for(int k=1;k<=p;k++)
                {
                    if(out[i][k]==0&&in[j][k]==1)
                    {
                        flag=0;
                        break;
                    }
                    if(out[i][k]==1&&in[j][k]==0)
                    {
                        flag=0;
                        break;
                    }
                }
//                cout<<i<<" "<<j<<" "<<flag<<endl;
                if(flag)
                    Dic.addedge(i+n,j,INF);
            }
        }
//        for(int i=0;i<Dic.edges.size();i++)
//        {
            Edge &e=Dic.edges[cut[i]];
//            cout<<Dic.edges[i].from<<"  "<<Dic.edges[i].to<<"  "<<Dic.edges[i].cap<<"  "<<Dic.edges[i].flow<<endl;
//        }
        int mx=Dic.Maxflow(s,t);
        ans.clear();
        int cnt=0;
        for(int i=0;i<Dic.edges.size();i++)
        {
            if(Dic.edges[i].from==0||Dic.edges[i].to==0)
                continue;
            if(Dic.edges[i].to==t||Dic.edges[i].from==t)
                continue;
            if((Dic.edges[i].from+n==Dic.edges[i].to)||(Dic.edges[i].from-n==Dic.edges[i].to))
                continue;
            if(Dic.edges[i].flow<0)
            {
                cnt++;
            }
        }
        printf("%d %d\n",mx,cnt);
        for(int i=0;i<Dic.edges.size();i++)
        {
            if(Dic.edges[i].from==0||Dic.edges[i].to==0)
                continue;
            if(Dic.edges[i].to==t||Dic.edges[i].from==t)
                continue;
            if((Dic.edges[i].from+n==Dic.edges[i].to)||(Dic.edges[i].from-n==Dic.edges[i].to))
                continue;
            if(Dic.edges[i].flow<0)
            {
                printf("%d %d %d\n",Dic.edges[i].to-n,Dic.edges[i].from,-Dic.edges[i].flow);
            }
        }
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值