poj 1273 Drainage Ditches (网络流_EK_入门)

本文介绍了如何使用广度优先搜索算法求解最大流问题,包括定义边数、点数、边容量、源点和汇点,通过实例演示了解决过程。

赤裸裸的网络流入门

给出了 边数, 点数, 边容量, 源点, 汇点. 求最大流

// poj 1273 EK   2012-05-31
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#define Bug cout << "here\n";
using namespace std;
const int INF = 0x7fffffff;
const int N = 205;
int map[N][N];
int link[N]; // 标记是否被访问,并且记录前一个点。
int mi[N];
int n, m;
int bfs(int s) {
    queue<int> Q;
    memset(link, 0, sizeof(link));
    mi[s] = INF;
    Q.push(s);
    int u, v;
    while(!Q.empty()) {
        u = Q.front();
        Q.pop();
        for(v = 2; v <= n; v++) {
            if(map[u][v] > 0 && !link[v]) {
                mi[v] = min(mi[u], map[u][v]);
                link[v] = u;
                if(v == n) return mi[n];
                Q.push(v);
            }
        }
    }
    return 0;
}
void cal() {
    int ans, now, pre, x;
    for(ans = 0; (x = bfs(1)) != 0; ans += x) {
        now = n;
        while(now != 1) {
            pre = link[now];
            map[pre][now] -= x;
            map[now][pre] += x;
            now = pre;
        }
    }
    printf("%d\n", ans);
}
int main() {
    int u, v, cost;
    while(scanf("%d%d", &m, &n) == 2) {
        memset(map, 0, sizeof(map));
        for(int i = 0; i < m; i++) {
            scanf("%d%d%d", &u, &v, &cost);
            map[u][v] += cost;
        }
        cal();
    }
    system("pause");
    return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值