hdu 3549 最大流(EK实现)

Problem: http://acm.hdu.edu.cn/showproblem.php?pid=3549

题目都直接说求最大流了,还需要做什么

数据这么弱,直接套最大流模版

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 16
int map[MAXN][MAXN],n,m,ans,pre[MAXN];//map[][]邻接矩阵存图,pre[]前驱数组 
bool EK_BFS(int start,int end){//宽搜寻增广路 
    queue<int> que;
    bool flag[MAXN];//标记点是否被选入队列 
    memset(flag,false,sizeof(flag));
    memset(pre,-1,sizeof(pre));//初始化前驱数组 
    que.push(start);
    flag[start]=true;
    while(!que.empty()){
        int e=que.front();
        if(e==end)return true;//当队头弹出的点为终点时 表示寻到增广路 
        que.pop();
        for(int i=1;i<=n;i++){
            if(map[e][i] && !flag[i]){
                flag[i]=true;
                pre[i]=e;
                que.push(i);
            }
        }
    }
    return false;
}
void work(){//EK_max_flow
    ans=0;//初始化最大流 
    while(EK_BFS(1,n)){
        int Minf=1010;
        int u=n;
        while(pre[u]!=-1){
            Minf=min(Minf,map[pre[u]][u]);//寻找瓶颈边 
            u=pre[u];
        }
        ans+=Minf;
        u=n;
        while(pre[u]!=-1){//修改路径上的边流量 
            map[pre[u]][u]-=Minf;
            map[u][pre[u]]+=Minf;
            u=pre[u];
        }
    }
}
void init(){
    scanf("%d%d",&n,&m);
    memset(map,0,sizeof(map));
    int x,y,c;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&x,&y,&c);
        map[x][y]+=c;//可能有重边 
    }
}
int main(){
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        init();
        work();
        printf("Case %d: %d\n",i,ans);
    }
}
View Code

相关知识传送门:http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117636.html

还不够...百度谷歌告诉你
图论网络流什么的,太赞了.....真的很好玩(很好玩的哟,你也要来玩吗?)

posted @ 2014-02-14 20:38  Cshhr  阅读(613)  评论(0编辑  收藏  举报