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;
}

猜你喜欢

转载自blog.csdn.net/flyzer/article/details/81259945