poj 1459 Power Network

来源:互联网 发布:网络电视盒软件哪个好 编辑:程序博客网 时间:2024/05/02 01:36

看别人的写的。

第一个网络流有关问题。

/* * Author: stormdpzh * POJ: 1459 Power Network * Time: 2012/4/26 13:07:31 */#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <vector>#include <queue>#include <stack>#include <set>#include <algorithm>#include <functional>#define sz(v) ((int)(v).size())#define rep(i, n) for(int i = 0; i < n; i++)#define repf(i, a, b) for(int i = a; i <= b; i++)#define out(n) printf("%d", n)#define wh(n) while(scanf("%d", &n) != EOF)#define whz(n) while(scanf("%d", &n) != EOF && n != 0)#define int64 long longusing namespace std;const int MaxN = 205;const int INF = (1 << 30);int f[MaxN][MaxN];int c[MaxN][MaxN];int p[MaxN], l[MaxN];queue<int> que;int n, np, nc, m;int solve(int s, int t){    while(!que.empty())        que.pop();    int res = 0;    while(true) {        memset(l, 0, sizeof(l));        que.push(s);        l[s] = INF;        while(!que.empty()) {            int u = que.front();            que.pop();            rep(v, n + 2) {                if(l[v] == 0 && c[u][v] > f[u][v]) {                    p[v] = u;                    l[v] = min(l[u], c[u][v] - f[u][v]);                    que.push(v);                }            }        }        if(l[t] == 0)            break;        for(int v = t; v != s; v = p[v]) {            f[p[v]][v] += l[t];            f[v][p[v]] -= l[t];        }        res += l[t];    }        return res;}int main(){    while(scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF) {        memset(f, 0, sizeof(f));        memset(c, 0, sizeof(c));        rep(i, m) {            int x, y, w;            char tmp;            cin >> tmp >> x >> tmp >> y >> tmp >> w;            c[x + 1][y + 1] = w;        }        rep(i, np) {            int x, w;            char tmp;            cin >> tmp >> x >> tmp >> w;            c[0][x + 1] = w;        }        rep(i, nc) {            int x, w;            char tmp;            cin >> tmp >> x >> tmp >> w;            c[x + 1][n + 1] = w;        }        printf("%d\n", solve(0, n + 1));    }    return 0;}