max_flow(Ford-Fulkerson) 分类: ACM TYPE 2014-09-02 01:50 110人阅读 评论(0) 收藏

时间:2023-01-29 19:40:43
#include <cstdio>
#include <iostream>
#include <cstring>
#include<queue>

using namespace std;
const int INF = 0x3fffffff;

int  g[1005][1005];
bool vis[1005];
int m;

int Ford(int s,int t,int f)
{
    if(s==t)    return f;
    for(int i=1;i<=m;i++)
    {
        if(vis[i] || g[s][i]==0)  continue;
        vis[i] = true;
        int d = Ford(i,t,min(f,g[s][i]));
        if(d>0)
        {
            g[s][i]-=d;
            g[i][s]+=d;
            return d;
        }
    }
    return 0;
}

int find_flows(int s,int t)
{
    int f = 0;
    while(1)
    {
        memset(vis,0,sizeof(vis));
        int flow = Ford(s,t,INF);
        if(flow == 0)   return f;
        f+=flow;
    }
}

int main()
{
    int T, a, b, len;
    int s, t, p;
        scanf("%d%d", &m, &p);
        scanf("%d%d",&s,&t);
        memset(g,0,sizeof(g));

        while(p--)
        {
            scanf("%d%d%d", &a, &b, &len);
            g[a][b] += len;
        }
        printf("%d\n", find_flows(s,t));
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。