POJ 3436 ACM Computer Factory(最大流)

POJ 3436 ACM Computer Factory(最大流)

ACM Computer Factory

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4077 Accepted: 1366 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

最近沉迷于做最大流(套板子真好玩),最大流的题最关键的在于建图,这题的题意大概是每一台机器都有自己的效率,输入,输出三个部分的量。

效率即为改机器每小时能够生产的数量

输入由P个组成,可能值为0,1,2,分别代表这P这部件在输入时的必要状态,1表示这个部件必须已经被装好,0表示这个部件必须没有被装好,2表示这个部件是否被装好无关紧要。

输出也由P个组成,可能值为0,1,分别代表这P这部件在输出时的状态,1表示这个部件在输出时被装好,0表示没有被装好。,

建图的思路任然为很经典的拆点建图,我们考虑将每一台机器拆成两个点(两个点分别继承其输入和输出属性),两点之间的流量为其效率Q

然后对超级源点s向输入不含1的点连边建图,对输出全为1的点向汇点t建图,这里的边容量均为无穷

最后我们考虑是否有机器的输出满足其他机器的输入,将满足这种条件的点也连边建图,边的容量为其输入效率

直接跑dinic玩事

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int INF1 = 1e9;
const int maxn=500+10;
int P,N;
int in[55][15],out[55][15],Q[55];
struct Edge {
    int from,to,cap,flow;
    Edge(){}
    Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
Edge ans[500];
struct Dinic
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    int d[maxn];
    int cur[maxn];
    bool vis[maxn];
 
    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;i++) G[i].clear();
    }
    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()
    {
        queue<int> Q;
        memset(vis,0,sizeof(vis));
        vis[s]=true;
        d[s]=0;
        Q.push(s);
        while(!Q.empty())
        {
            int x=Q.front(); Q.pop();
            for(int i=0;i<G[x].size();i++)
            {
                Edge e=edges[G[x][i]];
                if(!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to]=true;
                    d[e.to] = d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
 
    int DFS(int x,int a)
    {
        if(x==t || a==0) return a;
        int flow=0,f;
 
        for(int& i=cur[x];i<G[x].size();++i)
        {
            Edge& e=edges[G[x][i]];
            if(d[e.to]==d[x]+1 && (f=DFS(e.to, min(a,e.cap-e.flow) ) )>0 )
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }
 
    int Max_Flow()
    {
        int flow=0;
        while(BFS())
        {
            memset(cur,0,sizeof(cur));
            flow += DFS(s,INF1);
        }
        return flow;
    }
}DC;

int main() {
    scanf("%d%d",&P,&N);
    for(int i = 0;i < N;i++) {
        scanf("%d",&Q[i+1]);
        for(int j = 0;j < P;j++) scanf("%d",&in[i+1][j+1]);
        for(int j = 0;j < P;j++) scanf("%d",&out[i+1][j+1]);
    }    
    DC.init(2*N+2,0,2*N+1);
    for(int i = 1;i <= N;i++) DC.AddEdge(i,i+N,Q[i]);
    int flag1,flag2;
    for(int i = 1;i <= N;i++) {
        flag1 = flag2 = 1;
        for(int j = 1;j <= P;j++) {
            if(in[i][j] == 1) flag1 = 0;
        }
        for(int j = 1;j <= P;j++) {
            if(out[i][j] == 0) flag2 = 0;
        }
        if(flag1) DC.AddEdge(0,i,INF1);
        if(flag2) DC.AddEdge(i+N,2*N+1,INF1);
        for(int j = 1;j <= N;j++) {
            if(i != j) {
                int flag = 1;
                for(int k = 1;k <= P;k++) { 
                    if(out[i][k] != in[j][k] && in[j][k] != 2) flag = 0;
                }
                if(flag) DC.AddEdge(i+N,j,Q[i]);
            }
        }
    }
    printf("%d",DC.Max_Flow());
    int cnt = 0;
    for(int i = 0;i < DC.edges.size();i++) {
        if(DC.edges[i].flow > 0 && DC.edges[i].from > N && DC.edges[i].from < 2*N+1 && DC.edges[i].to > 0 && DC.edges[i].to < N+1) {ans[cnt++] = DC.edges[i];}
    }
    printf(" %d
",cnt);
    for(int i = 0;i < cnt;i++) {
        printf("%d %d %d
",ans[i].from - N,ans[i].to,ans[i].flow);
    }
    return 0;
}
我现在最大的问题就是人蠢而且还懒的一批。
原文地址:https://www.cnblogs.com/pot-a-to/p/11112818.html