• poj1459 Power Network *


    /*
    * 最大流 Edmonds-Karp
    *
    * 加入两个节点 S 和 T
    * S 与 所有 power stations相连 ; 所有 consumers 与 T 相连
    *
    */

    #include
    <iostream>
    #include
    <cstring>
    using namespace std;

    const int inf = 100000000;
    const int maxN = 100 + 5;
    int n, np, nc, m, l[maxN][maxN], s, t;
    int q[maxN], maxFlow, pre[maxN];

    void addFlow(){
    int minL = inf, cur = t;
    while(cur != s){
    if(l[pre[cur]][cur] < minL)
    minL
    = l[pre[cur]][cur];
    cur
    = pre[cur];
    }

    cur
    = t;
    while(cur != s){
    l[pre[cur]][cur]
    -= minL;
    l[cur][pre[cur]]
    += minL;
    cur
    = pre[cur];
    }

    maxFlow
    += minL;
    }


    void fulk(){
    while(true){
    memset(pre,
    -1, sizeof(pre));
    int head = 0, tail = 0, cur;
    q[tail
    ++] = s;

    while(head != tail){
    cur
    = q[head++];
    for(int j=0; j<=n+1; j++){
    if(l[cur][j] != 0 && pre[j] == -1){
    pre[j]
    = cur;
    q[tail
    ++] = j;
    }
    }
    if(pre[t] != -1) break;
    }
    if(pre[t] != -1) addFlow();
    else break;
    }
    }

    int main(){
    while(cin >> n){
    cin
    >> np >> nc >> m;

    memset(l,
    0, sizeof(l));
    s
    = 0, t = n + 1, maxFlow = 0;
    char tmp;
    int u, v, z;
    for(int i=1; i<=m; i++){
    cin
    >> tmp >> u >> tmp >> v >> tmp >> z;
    l[u
    +1][v+1] = z;
    }
    for(int i=1; i<=np; i++){
    cin
    >> tmp >> u >> tmp >> z;
    l[s][u
    +1] = z;
    }
    for(int i=1; i<=nc; i++){
    cin
    >> tmp >> u >> tmp >> z;
    l[u
    +1][t] = z;
    }


    fulk();

    cout
    << maxFlow << endl;




    }


    return 0;
    }
  • 相关阅读:
    BZOJ3672/UOJ7 [Noi2014]购票
    POJ3718 Facer's Chocolate Dream
    BZOJ1453:[WC]Dface双面棋盘
    BZOJ2957:楼房重建
    AtCoder Grand Contest 009 D:Uninity
    BZOJ2877:[NOI2012]魔幻棋盘
    BZOJ3065:带插入区间K小值
    BZOJ3489:A simple rmq problem
    浅谈主席树
    AtCoder Regular Contest 080 E:Young Maids
  • 原文地址:https://www.cnblogs.com/longdouhzt/p/2166203.html
Copyright © 2020-2023  润新知