HDU 3549

来源:互联网 发布:java内存泄露例子 编辑:程序博客网 时间:2024/06/06 03:06

#include<iostream>#include<queue>#include<cstdio>#include<cstring>using namespace std;int res[20][20], vis[20], pre[20];queue<int>q;int N, M;bool bds(int s, int t){    memset(vis, 0, sizeof(vis));    memset(pre, -1, sizeof(pre));    while(!q.empty())        q.pop();    q.push(s);    vis[s] = 1;    while(!q.empty())    {        int p = q.front();        q.pop();        for(int i = 1;i <= N;i ++)        {            if(!vis[i] && res[p][i] > 0)            {                vis[i] = 1;                pre[i] = p;                q.push(i);                if(i == t)                    return true;            }        }    }    return false;}int EK(int s, int t){    int flow = 0, d, i, u;    while(bds(s, t))    {        d = 1 << 30;        u = t;        while(pre[u] != -1)        {            d = min(d, res[pre[u]][u]);            u = pre[u];        }        u = t;        while(pre[u] != -1)        {            res[pre[u]][u] -= d;            res[u][pre[u]] += d;            u = pre[u];        }        flow += d;    }    return flow;}int main(int argc, char const *argv[]) {    int T, u, v, w, cnt = 0;    //freopen("in.c", "r", stdin);    scanf("%d", &T);    while(T--)    {        memset(res, 0, sizeof(res));        scanf("%d%d", &N, &M);        for(int i = 0;i < M;i ++)        {            scanf("%d%d%d", &u, &v, &w);            res[u][v] += w;        }        printf("Case %d: %d\n", ++cnt, EK(1, N));    }    return 0;}


0 0