poj-1273-Drainage Ditches(最大流问题)

传送门

经典的最大流问题,不明白的可以在网上找找相关博客看看
Edmonds Karp算法的模板题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define ll long long 
#define N 205
#define MAX 111111111
using namespace std;
int flow[N][N], n, m, pre[N], capacity[N][N];
int Augmenting_Path(int src, int des, int num){//bfs寻找增广链,如果找,返回这条增广链的容量,否则返回-1 
    int i, j, ret = MAX;
    memset(pre, -1, sizeof(pre));
    queue<int> q;
    q.push(src);
    pre[src] = 0;
    while(!q.empty()){
        int tmp = q.front();
        q.pop();
        for (i = 1; i <= num; i++){
            if (pre[i] == -1 && flow[tmp][i] < capacity[tmp][i]){
                q.push(i);
                pre[i] = tmp;
                ret = min(ret, capacity[tmp][i]-flow[tmp][i]);
                if (i == des)   return ret;
            }
        }
    }
    return -1;
}
int Edmonds_Karp(int src, int des, int num){//标号法寻找最大流量 src:原点 des:终点 num:结点数 
    int i, j, k, ret = 0;
    while(1){
        int Min = Augmenting_Path(src, des, num);
        if (Min == -1){
            return ret;
        }
        i = des;
        while(i != src){
            flow[pre[i]][i] += Min;
            flow[i][pre[i]] -= Min;
            i = pre[i];
        }
        ret += Min;
    }
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif 
    int i, j, a, b, v;
    while(~scanf("%d%d", &m, &n)){
        memset(capacity, 0, sizeof(capacity));
        memset(flow, 0, sizeof(flow));
        for (i = 0; i < m; i++){
            scanf("%d%d%d", &a, &b, &v);
            capacity[a][b] += v;
        }
        printf("%d\n", Edmonds_Karp(1, n, n));
    }
    return 0;
}

你可能感兴趣的:(poj-1273-Drainage Ditches(最大流问题))