码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-3549 最大流模板题

时间:2016-08-10 22:28:21      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1、HDU-3549   Flow Problem

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

 

3、总结:模板题,参考了 http://www.cnblogs.com/Lyush/archive/2011/08/08/2130660.html  ,里面有几种模板,没太看懂

 

题意:给定有向图,求第1到第n个点的最大流。

技术分享
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int N=100100;

int mapn[20][20],flow[20][20];     //mapn存储边最大容量,flow存储边实际容量
int path[20],pre[20];       //path存储当前结点流量,pre存储父亲路径
int n,m;

int bfs()
{
    memset(path,0,sizeof(path));
    memset(pre,0,sizeof(pre));

    queue<int>q;
    q.push(1);
    int pos;
    path[1]=INF;    //下面要比较到path[1]
    while(!q.empty())
    {
        pos=q.front();
        q.pop();
        for(int i=1;i<=n;i++)
        {
            if(!path[i]&&flow[pos][i]<mapn[pos][i]){
                //如果这条边没有饱和或者形成了回流,就更新当前结点的流量

                path[i]=min(path[pos],mapn[pos][i]-flow[pos][i]);
                //即path[i]要取这条增广路径上残余流量的最小值

                pre[i]=pos;     //记录下父亲路径
                q.push(i);
            }
        }
    }

    return path[n];
}

int Max_Flow()
{
    int ans=0,pos,path_n;
    memset(flow,0,sizeof(flow));

    while(true)
    {
        path_n=bfs();      //每一次bfs获得一条增广路径
        if(!path_n)return ans;      //如果找不到增广路径就跳出
        pos=n;
        while(pos!=1){     //更新flow
            flow[pre[pos]][pos]+=path_n;
            flow[pos][pre[pos]]-=path_n;
            pos=pre[pos];
        }
        ans+=path_n;
    }

}

int main()
{
    int t;
    scanf("%d",&t);
    for(int test=1;test<=t;test++)
    {
        memset(mapn,0,sizeof(mapn));
        scanf("%d%d",&n,&m);
        int x,y,c;
        while(m--){
            scanf("%d%d%d",&x,&y,&c);
            mapn[x][y]+=c;
        }

        int max_flow;
        max_flow=Max_Flow();
        printf("Case %d: %d\n",test,max_flow);

    }

    return 0;
}
View Code

 

HDU-3549 最大流模板题

标签:

原文地址:http://www.cnblogs.com/sbfhy/p/5758942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!