Fork me on GitHub

hdu 3549 Flow problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

题意分析:最大网络流 模板题

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<algorithm>

#define INF 0x7fffffff
#define N 100

using namespace std;

int map[N][N],v[N];
int pre[N];
int n,m;

int BFS(int s,int t)
{
    queue<int>q;
    memset(pre,-1,sizeof(pre));
    memset(v,0,sizeof(v));
    pre[s] = s;
    v[s] = 1;
    q.push(s);
    int i;
    while(!q.empty())
    {
        int w = q.front();
        q.pop();
        for(i=1;i<=n;i++)
        {
            if(map[w][i]&&v[i] == 0)
            {
                pre[i] = w;
                v[i] = 1;
                if(i == t)
                {
                    return 1;
                }
                q.push(i);
            }
        }
    }
    return 0;
}

int EK(int s,int t)
{
    int count = 0;
    int i,pp;
    while(BFS(s,t))
    {
        pp = INF;
        for(i=t;i!=s;i = pre[i])
        {
            if(pp>=map[pre[i]][i])
            {
                pp = map[pre[i]][i];
            }
        }
        for(i=t;i!=s;i=pre[i])
        {
            map[pre[i]][i] -= pp;
            map[i][pre[i]] += pp;
        }
        count += pp;
    }
    return count;
}

int main()
{
    int T,t=0,i,j;
    scanf("%d",&T);
    while(T--)
    {
        t++;
        int x,y,z;
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            map[x][y] += z;
        }
        printf("Case %d: %d\n",t,EK(1,n));
    }
    return 0;
}

 

posted @ 2015-02-27 10:59  I'm coding  阅读(248)  评论(0编辑  收藏  举报