hdu1532 Drainage Ditches(E-K最大流)

题意:

输入m n,   m是边数,n是点数。 接下来m行:   起点,终点,最大流量。求以 1 为源点, n为汇点的最大流。
    #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 205;
int pre[maxn],mark[maxn],len[maxn][maxn];
int n,m,u,v,l,f;
void max_low(){
    while(1){
        ms(mark,0);
        ms(pre,0);
        queue<int> q;
        q.push(1);
        mark[1]=1;
        int cnt;
        while(!q.empty()){          //不断寻找增广路
            cnt = q.front();
            q.pop();
            if(cnt==m)
                break;
            for(int i=1;i<=m;i++){
                if(!mark[i]&&len[cnt][i]>0){
                    mark[i] = 1;
                    pre[i] = cnt;
                    q.push(i);
                }
            }

        }
        if(!mark[m]) break;
        int minx = inf;
        for(int i=m;i!=1;i=pre[i]){         //寻在最大流量
            minx=min(minx,len[pre[i]][i]);
        }
        for(int i=m;i!=1;i=pre[i]){         //更新流量
            len[pre[i]][i] -= minx;
            len[i][pre[i]] += minx;
        }
        f += minx;
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        f = 0;
        ms(len,0);
        while(n--){
            scanf("%d%d%d",&u,&v,&l);
            len[u][v] += l;
        }
        max_low();
        printf("%d\n",f);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1561067921/article/details/77875951
今日推荐