Poj3436 网络流

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10431 Accepted: 3885 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.
Source

Northeastern Europe 2005, Far-Eastern Subregion

题意:你有n台机器,你需要组装p个配件,每台机器对配件都有要求,用数组input表示,数字0,代表不得存在这个配件,数字1代表必需存在这个配件,数字2代表无所谓,将这些配件交给机器,机器会都组装出一些配件,用数组output表示,0代表没有这个配件,1代表有这个配件。

思路:由案例可以看出,多台机器可以作为起点,多台机器可以作为终点,这样用最大流算法不好操作,所以我们可以自己创建一个起点st,将st指向可以作为起点的所有机器,再创建一个终点en,将所有可以作为终点的机器指向en
解决了起点和终点的问题,就剩下权值问题了,因为每台机器都有一个权值,所以,两台机器之间必须有两条路,比如设中间点为i+n,那么两条路就是i–>i+n 权值为w[i] i+n–>j 权值为w[j],这样权值问题就解决了,
接下来就是一个网络流裸题了,只是要输出要下路径

AC代码:

//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
struct node
{
    int v,next,w;
}edge[Max];
struct cm
{
    int in[15],out[15],w;
}a[Max];
struct ans
{
    int st,en,w;
};
int dis[Max],head[Max],cur[Max];
int index;
int st,en;
int n,m;
queue<int>p;
queue<ans>q;
stack<ans>sa;
ans s;
bool checks(int i){
    for(int j=0;j<m;j++)
        if(a[i].in[j]==0 || a[i].in[j]==2)
            ;
        else
            return false;
    return true;
}
bool checke(int i){
     for(int j=1;j<m;j++)
        if(a[i].out[j-1]==a[i].out[j] && a[i].out[j]==1)
            ;
        else
            return false;
    return true;
}
void init(){
    index=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w){
    edge[index].v=v,edge[index].w=w;
    edge[index].next=head[u],head[u]=index++;

    edge[index].v=u,edge[index].w=0;
    edge[index].next=head[v],head[v]=index++;
}
bool bfs(){
    memset(dis,0,sizeof(dis));
    while(p.size())
        p.pop();
    dis[st]=1;
    p.push(st);
    while(p.size()){
        int top=p.front();
        p.pop();
        for(int t=head[top];t!=-1;t=edge[t].next)
            if(dis[edge[t].v]==0 && edge[t].w>0){
                dis[edge[t].v]=dis[top]+1,p.push(edge[t].v);
            }
    }
    return dis[en]!=0;
}
int dfs(int u,int flow){
    if(u==en)
        return flow;
    for(int& t=cur[u];t!=-1;t=edge[t].next){
        if(dis[edge[t].v]==dis[u]+1 && edge[t].w>0){
            int mn=dfs(edge[t].v,min(flow,edge[t].w));
            if(mn>0){
                edge[t].w-=mn;
                edge[t^1].w+=mn;
                s.st=u,s.en=edge[t].v,s.w=mn;
                q.push(s);
                return mn;
            }
        }
    }
    return 0;
}
int Dinic()
{
    LL ans=0;
    while(bfs()){
        for(int i=0;i<=2*n+1;i++)
            cur[i]=head[i];
        while(int mn=dfs(st,INT_MAX)){
            ans+=mn;
        }

    }
    return ans;
}
bool check(int s,int e){
    for(int i=0;i<m;i++){
        if(a[s].out[i]==a[e].in[i] || a[e].in[i]==2 )
            ;
        else
            return false;
    }
    return true;
}
void build(){
     for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j)
                    continue;
                if(check(i,j)){
                    add(i+n,j,a[j].w);
                }
            }
        }
}
int main()
{
    while(scanf("%d%d",&m,&n)==2){
        init();
        int res=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i].w);
            for(int j=0;j<m;j++)
                scanf("%d",&a[i].in[j]);
            for(int j=0;j<m;j++)
                scanf("%d",&a[i].out[j]);
            add(i,i+n,a[i].w);
             if(checks(i)){
                 add(0,i,a[i].w);
             }
             if(checke(i)){
                add(i+n,2*n+1,a[i].w);
            }
        }
        build();
        st=0,en=2*n+1;
        res=Dinic();
        while(q.size()){
            s=q.front();
            q.pop();
            if(s.st==0 || s.en==2*n+1)
                ;
            else if(s.st>n){
                s.st-=n;
                sa.push(s);
            }
        }
        printf("%d %d\n",res,sa.size());
        while(sa.size()){
            s=sa.top();
            sa.pop();
            printf("%d %d %d\n",s.st,s.en,s.w);
        }
    }
    return 0;
}

其实第二步可以不要,只要每次加边的时候比较最小值就好
AC代码:

//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
struct node
{
    int v,next,w;
}edge[Max];
struct cm
{
    int in[15],out[15],w;
}a[Max];
struct ans
{
    int st,en,w;
};
int dis[Max],head[Max],cur[Max];
int index;
int st,en;
int n,m;
queue<int>p;
queue<ans>q;
stack<ans>sa;
ans s;
bool checks(int i){
    for(int j=0;j<m;j++)
        if(a[i].in[j]==0 || a[i].in[j]==2)
            ;
        else
            return false;
    return true;
}
bool checke(int i){
     for(int j=1;j<m;j++)
        if(a[i].out[j-1]==a[i].out[j] && a[i].out[j]==1)
            ;
        else
            return false;
    return true;
}
void init(){
    index=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w){
    edge[index].v=v,edge[index].w=w;
    edge[index].next=head[u],head[u]=index++;

    edge[index].v=u,edge[index].w=0;
    edge[index].next=head[v],head[v]=index++;
}
bool bfs(){
    memset(dis,0,sizeof(dis));
    while(p.size())
        p.pop();
    dis[st]=1;
    p.push(st);
    while(p.size()){
        int top=p.front();
        p.pop();
        for(int t=head[top];t!=-1;t=edge[t].next)
            if(dis[edge[t].v]==0 && edge[t].w>0){
                dis[edge[t].v]=dis[top]+1,p.push(edge[t].v);
               // printf("%d %d\n",top,edge[t].v);
            }

    }
    return dis[en]!=0;
}
int dfs(int u,int flow){
    if(u==en)
        return flow;
    for(int& t=cur[u];t!=-1;t=edge[t].next){
        if(dis[edge[t].v]==dis[u]+1 && edge[t].w>0){
            int mn=dfs(edge[t].v,min(flow,edge[t].w));
            if(mn>0){
                edge[t].w-=mn;
                edge[t^1].w+=mn;
                s.st=u,s.en=edge[t].v,s.w=mn;
                q.push(s);
                return mn;
            }
        }
    }
    return 0;
}
int Dinic()
{
    LL ans=0;
    while(bfs()){
        for(int i=1;i<=n+2;i++)
            cur[i]=head[i];
        while(int mn=dfs(st,INT_MAX)){
            ans+=mn;
        }

    }
    return ans;
}
bool check(int s,int e){
    for(int i=0;i<m;i++){
        if(a[s].out[i]==a[e].in[i] || a[e].in[i]==2 )
            ;
        else
            return false;
    }
    return true;
}
void build(){
     for(int i=2;i<=n+1;i++){
            for(int j=2;j<=n+2;j++){
                if(i==j)
                    continue;
                if(check(i,j)){
                    add(i,j,min(a[j].w,a[i].w));
                }
            }
        }
}
int main()
{
    while(scanf("%d%d",&m,&n)==2){
        init();
        int res=0;
        for(int i=2;i<=n+1;i++){
            scanf("%d",&a[i].w);
            for(int j=0;j<m;j++)
                scanf("%d",&a[i].in[j]);
            for(int j=0;j<m;j++)
                scanf("%d",&a[i].out[j]);
            //add(i,i+n,a[i].w);
             if(checks(i)){
                 add(1,i,a[i].w);
             }
             if(checke(i)){
                add(i,n+2,a[i].w);
            }
        }
        build();
        st=1,en=n+2;
        res=Dinic();
        //printf("%d\n",q.size());
        while(q.size()){
            s=q.front();
            q.pop();
            if(s.st==1 || s.en==n+2)
                ;
            else{
                s.st-=1;
                s.en-=1;
                sa.push(s);
            }
        }
        printf("%d %d\n",res,sa.size());
        while(sa.size()){
            s=sa.top();
            sa.pop();
            printf("%d %d %d\n",s.st,s.en,s.w);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值