hdu 1532 最大流入门题

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

题意:

农田在1点,河流在n点,中间有一些通路,问从1到n的最大流量

分析:

最大流的入门题

#include<iostream>
#include <algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int INF=100000000;
const int N=209;
struct edge
{
    int from,to,cap,flow; //两个端点,最大容量和流量
    edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n;
vector<edge> e; //存放所有的边
vector<int>g[N]; //记录每个点对应的边集
int a[N],p[N]; //分别记录路径上的最小残量和路径
void init()
{
    for(int i=0;i<=n;i++)g[i].clear();
    e.clear();
}
void addedge(int from,int to,int cap)
{
    e.push_back(edge(from,to,cap,0));
    e.push_back(edge(to,from,0,0));
    int m=e.size();
    g[from].push_back(m-2);
    g[to].push_back(m-1);
}
int maxflow(int s,int t)
{
    int flow=0;
    while(1){
        memset(a,0,sizeof(a));
        queue<int>q;
        q.push(s);
        a[s]=INF;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<g[x].size();i++){
                edge& t=e[g[x][i]];
                if(!a[t.to]&&t.cap>t.flow){
                    p[t.to]=g[x][i];
                    a[t.to]=min(a[x],t.cap-t.flow);
                    q.push(t.to);
                }
            }
            if(a[t])break;
        }
        if(!a[t])break;
        for(int u=t;u!=s;u=e[p[u]].from){
            e[p[u]].flow+=a[t];
            e[p[u]^1].flow-=a[t];
        }
        flow+=a[t];
    }
    return flow;
}
int main()
{
    int m;
    while(~scanf("%d%d",&m,&n)){
        init();
        int u,v,w;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        printf("%d\n",maxflow(1,n));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值