POJ 3436 ACM Computer Factory 最大流

來源:互聯網
上載者:User

標籤:

要求輸出每一條有流量的邊的流量,資料範圍不大我就用標號法水過了,輸出的時候只要把所有大於0的流量的邊輸出就好。

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxp = 15;const int maxn = 55;const int INF = INT_MAX / 5;int mcin[maxp][maxn],mcout[maxp][maxn];int cap[maxn][maxn],flow[maxn][maxn],Q[maxn];int p,n,s,t;void input() {    memset(cap,0,sizeof(cap));    for(int i = 1;i <= n;i++) {        scanf("%d",&Q[i]);        for(int j = 1;j <= p;j++)            scanf("%d",&mcin[i][j]);        for(int j = 1;j <= p;j++) {            scanf("%d",&mcout[i][j]);        }    }    s = 0,t = n + 1;}bool cantrans(int a,int b) {    for(int i = 1;i <= p;i++) {        if(mcout[a][i] == 1 && mcin[b][i] == 0) return false;         if(mcout[a][i] == 0 && mcin[b][i] == 1) return false;    }    return true;}void build_graph() {    for(int i = 1;i <= n;i++) {        for(int j = 1;j <= n;j++) if(i != j) {            if(cantrans(i,j) && j != s) {                cap[i][j] = Q[i];            } else cap[i][j] = 0;        }    }    for(int i = 1;i <= n;i++) {        int sumout = 0,sumin = 0;        for(int j = 1;j <= p;j++) {            sumout += mcout[i][j];            sumin += mcin[i][j];        }        if(sumin == 0 || sumin == 2 * p) cap[s][i] = Q[i];        if(sumout == p) cap[i][t] = Q[i];    }}int q[maxn * 2],qs,qe;int pre[maxn],alpha[maxn];void solve() {    build_graph();    memset(flow,0,sizeof(flow));    while(1) {        qs = qe = 0;        q[qe++] = s;        for(int i = s;i <= t;i++) pre[i] = -2;        pre[s] = -1; alpha[s] = INF;        while(qs < qe) {            int now = q[qs++];            for(int i = s;i <= t;i++) if(cap[now][i] - flow[now][i] && pre[i] == -2) {                q[qe++] = i; pre[i] = now;                 alpha[i] = min(alpha[now],cap[now][i] - flow[now][i]);            }        }        if(pre[t] == -2) break;        for(int i = t;pre[i] != -1;i = pre[i]) {            flow[pre[i]][i] += alpha[t];            flow[i][pre[i]] -= alpha[t];        }    }    int ans = 0,ecnt = 0;    for(int i = 1;i <= n;i++) ans += flow[i][t];    for(int i = 1;i <= n;i++) {        for(int j = 1;j <= n;j++) {            if(flow[i][j] > 0) ecnt++;        }    }    printf("%d %d\n",ans,ecnt);    for(int i = 1;i <= n;i++) {        for(int j = 1;j <= n;j++) {            if(flow[i][j] > 0) printf("%d %d %d\n",i,j,flow[i][j]);        }    }}int main() {    while(scanf("%d%d",&p,&n) != EOF) {        input();        solve();    }    return 0;}

  

POJ 3436 ACM Computer Factory 最大流

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.