hdu 3549 Flow Problem(图论:网络流增广路)

来源:互联网 发布:2016年淘宝店很难开吗 编辑:程序博客网 时间:2024/05/21 10:47

一道模板题,直接把白书上的模板往上贴,学会使用模板即可

代码如下:

#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAXN 2000#define LL long long#define INF 0x7fffffffusing namespace std;int n;int flow[20][20], p[MAXN];int a[MAXN], cap[20][20];int Karp(int s, int t) {    queue<int> q;    memset(flow, 0, sizeof(flow));    int f = 0;    while(true) {        memset(a, 0, sizeof(a));        a[s] = INF;        q.push(s);        while(!q.empty()) {            int u = q.front();            q.pop();            for(int v=1; v<=n; ++v) {                if(!a[v] && cap[u][v]>flow[u][v]) {                    p[v] = u;                    q.push(v);                    a[v] = min(a[u], cap[u][v]-flow[u][v]);                }            }        }       if(a[t] == 0)            break;        for(int u=t; u!=s; u=p[u]) {            flow[p[u]][u] += a[t];            flow[u][p[u]] -= a[t];        }        f += a[t];    }    return f;}int main(void) {    int T, m, x, y, t, w;    scanf("%d", &T);    for(t=1; t<=T; ++t) {        scanf("%d%d", &n, &m);        memset(cap, 0, sizeof(cap));        while(m--) {            scanf("%d%d%d", &x, &y, &w);            cap[x][y] += w;        }        printf("Case %d: %d\n", t, Karp(1, n));    }    return 0;}


0 0
原创粉丝点击