POJ 1459 Power Network 最大流

來源:互聯網
上載者:User

標籤:blog   os   io   for   div   ar   amp   type   

建模不難,就讀入有點麻煩,無腦拍完dinic 1A happy~

#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 maxn  = 105;int n,np,nc,m; //n節點數目,np電站數目,nc消費者數目,m電線int cap[maxn][maxn],s,t;        //cap 容量,s源點,t匯點int level[maxn],q[maxn * 2],qs,qe;void input() {    memset(cap,0,sizeof(cap));    s = 0; t = n + 1;    for(int i = 0;i < m;i++) {        int a,b,L; scanf(" (%d,%d)%d",&a,&b,&L);        a++; b++;        cap[a][b] = L;    }    for(int i = 0;i < np;i++) {        int a,P; scanf(" (%d)%d",&a,&P); a++;        cap[s][a] = P;    }    for(int i = 0;i < nc;i++) {        int a,C; scanf(" (%d)%d",&a,&C); a++;        cap[a][t] = C;    }    /*    printf("\n%d %d\n",s,t);    for(int i = s;i <= t;i++) {        for(int j = s;j <= t;j++) {            printf("%d ",cap[i][j]);        }        putchar(‘\n‘);    }    */}bool bfs() {    qs = qe = 0;    q[qe++] = s;    memset(level,0,sizeof(level));    level[s] = 1;    while(qs < qe) {        int v = q[qs++];        if(v == t) break;        for(int i = s;i <= t;i++) if(cap[v][i] && !level[i]) {            level[i] = level[v] + 1;            q[qe++] = i;        }    }    return level[t];}int dinic(int now,int alpha) {    int sum = 0;    if(now == t) return alpha;    for(int i = s;i <= t;i++) {        if(level[i] == level[now] + 1 && alpha && cap[now][i]) {            int ret = dinic(i,min(alpha,cap[now][i]));            cap[now][i] -= ret;            cap[i][now] += ret;            alpha -= ret;            sum += ret;        }    }    return sum;}void solve() {    int ans = 0;    while(bfs()) ans += dinic(s,INT_MAX);    printf("%d\n",ans);}int main() {    while(scanf("%d%d%d%d",&n,&np,&nc,&m) != EOF) {        input();        solve();    }    return 0;}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.