Poj3436 ACM computer factory, maximum stream, output path

Source: Internet
Author: User
Poj 3436 ACM computer factory

Computer companies have n computers, each of which generates Qi in a unit of time.


A computer consists of P parts. Each machine can only work on a semi-finished computer with some parts (or an empty computer with nothing) it turns into a semi-finished computer or a complete computer with other parts (or some parts may be removed ). Ask the computer company for the maximum unit time output, and which machines have a cooperative relationship, that is, the machines on which one machine delivers its products for processing.


Sample Input
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 output
25 2
1 3 15
2 3 10

Input: The computer consists of three parts. There are 4 machines in total. The output of machine 1 is 15. You can add Part 2 to the empty computer, machine 2 can add Part 2 and Part 3 to an empty computer, machine 3 can turn a computer with both Part 2 and Part 3 into a finished product (each part has one)


Output: the maximum output per unit time is 25. Two machines have a cooperative relationship,
Machine 1 needs to process 15 computers to machine 3 within the unit time
Machine 2 needs to process 10 computers to machine 3 within the unit time



The data size is small. You can use edmondskarp. The main question is not only the maximum flow value, but also the edge with flow.

Think about it carefully. You don't need to split this question!


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>using namespace std;typedef long long LL;const int maxn = 60;const int inf = 0x7fffffff;struct Edge {    int from,to,cap,flow;    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f) {}};struct EdmondsKarp {    int n,m;    vector<Edge> edges;    vector<int> G[maxn];    int p[maxn];    int a[maxn];    void init(int n)    {        this->n=n;        for(int i=0; i<n; i++)            G[i].clear();        edges.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);    }    int Maxflow(int s,int t)    {        int flow=0;        while(true) {            memset(a,0,sizeof(a));            queue<int> Q;            Q.push(s);            a[s]=inf;            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(!a[e.to]&&e.cap>e.flow) {                        a[e.to]=min(a[x],e.cap-e.flow);                        p[e.to]=G[x][i];                        Q.push(e.to);                        if(e.to==t)                            break;                    }                }                if(a[t])break;            }            if(!a[t])break;            for(int u=t; u!=s; u=edges[p[u]].from) {                edges[p[u]].flow+=a[t];                edges[p[u]^1].flow-=a[t];            }            flow+=a[t];        }        return flow;    }};EdmondsKarp solver;int P, N;int w[maxn], in[maxn][12], out[maxn][12];int print[maxn][5], cnt;int main(){    int i, j;    bool flag;    while(~scanf("%d%d", &P, &N)) {        int s = 0, t = N+1;        solver.init(N+2);        for(int i=1; i<=N; ++i) {            scanf("%d", &w[i]);            flag = true;            for(j=0; j<P; ++j) {                scanf("%d", &in[i][j]);                if(in[i][j]==1) flag = false;            }            if(flag) solver.AddEdge(s, i, w[i]);            flag = true;            for(j=0; j<P; ++j) {                scanf("%d", &out[i][j]);                if(out[i][j]!=1) flag = false;            }            if(flag) solver.AddEdge(i, t, w[i]);        }        for(i=1; i<=N; ++i) {            for(j=1; j<=N; ++j) {                if(i==j) continue;                flag = true;                for(int k=0; k<P; ++k)                    if(out[i][k] + in[j][k] == 1) {                        flag = false;                        break;                    }                if(flag) solver.AddEdge(i, j, min(w[i], w[j]));            }        }        printf("%d ", solver.Maxflow(s, t));        cnt = 0;        for(i=1; i<=N; ++i)            for(j=0; j<solver.G[i].size(); ++j)        {            Edge &e = solver.edges[solver.G[i][j]];            if(e.flow>0 && e.to != t && e.from != s)            {                print[cnt][0] = e.from;                print[cnt][1] = e.to;                print[cnt++][2] = e.flow;            }        }        printf("%d\n", cnt);        for(i=0; i<cnt; ++i)            printf("%d %d %d\n", print[i][0],print[i][1],print[i][2]);    }    return 0;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.