hdu 1532 最大流(模板)

题目:http://acm.split.hdu.edu.cn/showproblem.php?pid=1532

题意:第一行n和m,下面n行表示有向图,有m个点

E-K:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int maxn=500;
const int INF=2110000000;
int visited[maxn],pre[maxn],n,m;
int map[maxn][maxn];

void update_residual_network(int u,int flow){
    while(pre[u]!=-1){
        map[pre[u]][u]-=flow;
        map[u][pre[u]]+=flow;
        //printf(" %d-%d",u,pre[u]);
        u=pre[u];
    }
}
int find_path_bfs(int s,int t){
    memset(visited,0,sizeof(visited));
    memset(pre,-1,sizeof(pre));
    visited[s]=1;
    int min=INF;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int cur=q.front();q.pop();
        if(cur==t)
            break;
        for(int  i = 1 ; i <= m ; i++ ){
            if( visited[i] == 0 && map[cur][i] != 0){
                q.push(i);
                min=(min<map[cur][i]?min:map[cur][i]) ;
                pre[i]=cur;
                visited[i]=1;
            }
        }
    }
    if(pre[t]==-1)
        return 0;
    return min;
}
int edmonds_karp(int s,int t){
    int new_flow=0;
    int max_flow=0;
    do{
        new_flow = find_path_bfs(s,t);
        update_residual_network(t,new_flow);
        max_flow += new_flow;
        //printf(" max=%d",new_flow);
    }while( new_flow != 0 );
    return max_flow;
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(map,0,sizeof(map));
        int x,y,val;
        for(int i=0;i<n;i++){
           scanf("%d%d%d",&x,&y,&val);
           map[x][y]+=val;
        }
        printf("%d\n",edmonds_karp(1,m));
    }
    return 0;
}

Dinic:

//Dinic算法模板 白书358页,点的编号从0开始
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=202;
const int inf=0x7fffffff;
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>g[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void Init(int n){
        this->n=n;
        for(int i=0;i<n;i++) g[i].clear();
        edges.clear();
    }
    void Addedge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));//反向弧
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool Bfs(){
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<(int)g[x].size();i++){
                Edge &e=edges[g[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int Dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int&i=cur[x];i<(int)g[x].size();i++){
            Edge &e=edges[g[x][i]];
            if(d[x]+1==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[g[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t){
        this->s=s;this->t=t;
        int flow=0;
        while(Bfs()){
            memset(cur,0,sizeof(cur));
            flow+=Dfs(s,inf);
        }
        return flow;
    }
}dc;
int main()
{
    int n,m,a,b,c;
    while(scanf("%d%d",&n,&m)==2){
        dc.Init(m);
        while(n--){
            scanf("%d%d%d",&a,&b,&c);
            a--;b--;
            dc.Addedge(a,b,c);
        }
        printf("%d\n",dc.Maxflow(0,m-1));
    }
    return 0;
}

转自: http://www.cnblogs.com/--ZHIYUAN/p/6369229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值