HDU 1532 网络流裸题

转自:https://blog.csdn.net/qq_38987374/article/details/80083754

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

标准的网络流裸题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define LL long long
#define inf 10000010
using namespace std;
queue<int> q;
int c[210][210],f[210],pre[210];
int n,m;
int bfs(int src,int des)
{
    while(!q.empty())
        q.pop();
    memset(pre,-1,sizeof pre);
    f[src]=inf;
    q.push(src);
    int now;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now==des)
            break;
        for(int next=1; next<=m; next++)
        {
            if(next==src||c[now][next]<=0||pre[next]!=-1)
                continue;
            pre[next]=now;
            f[next]=min(f[now],c[now][next]);
            q.push(next);
        }
    }
    if(pre[des]==-1)      return -1;
    else    return f[des];
}
int maxflow(int src,int des)
{
    int cnt,ans=0;
    int front,now;
    while((cnt=bfs(src,des))!=-1)
    {
        front=pre[des];
        now=des;
        while(front!=-1)
        {
            c[front][now]-=cnt;
            c[now][front]+=cnt;
            now=front;
            front=pre[now];
        }
        ans+=cnt;
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(c,0,sizeof c);
        int u,v,val;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&u,&v,&val);
            if(u==v)
                continue;
            else
                c[u][v]+=val;
        }
        cout<<maxflow(1,m)<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值