POJ 1273 - Drainage Ditches (网络流)

解决农场水涝问题,通过构建排水系统确保水流顺畅。使用Edmonds-Karp算法和Dinic算法找到最大流,保证水能有效排出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 55767 Accepted: 21378

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

Source

[Submit]   [Go Back]   [Status]   [Discuss]




第一次写网络流 记录一下

Edmonds-Karp 最短增广路算法


/*************************************************************************
	> Name: ./test1.cpp
	> Created Time: 2014年08月10日 星期日 13时04分13秒
 ************************************************************************/

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 200 + 20;
int n, m;
int G[maxn][maxn];
int Prev[maxn];
int Visted[maxn];

int Augment(int st, int ed) {
    deque<int> Q;
    memset(Visted, 0, sizeof(Visted));
    memset(Prev, 0, sizeof(Prev));
    Prev[st] = 0;
    Visted[st] = st;
    Q.push_back(st);
    bool findPath = false;
    while(!Q.empty()) {
        int v = Q.front();
        Q.pop_front();
        for(int i=1; i<=ed; i++) {
            if(G[v][i] > 0 && !Visted[i]) {
                Prev[i] = v;
                Visted[i] = 1;
                if(i == ed) {
                    findPath = true;
                    Q.clear();
                    break;
                } else {
                    Q.push_back(i);
                }
            }
        }
    }
    if(!findPath) return 0;
    int minFlow = INF;
    int v = ed;
    while(Prev[v]) {
        minFlow = min(minFlow, G[Prev[v]][v]);
        v = Prev[v];
    }
    v = ed;
    while(Prev[v]) {
        G[Prev[v]][v] -= minFlow;
        G[v][Prev[v]] += minFlow;
        v = Prev[v];
    }
    return minFlow;
}

int main() {

    while(scanf("%d%d", &m, &n) != EOF) {
        memset(G, 0, sizeof(G));
        for(int i=0; i<m; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            G[u][v] += w;
        }
        int ans = 0, aug;
        while(aug = Augment(1, n)) {
            ans += aug;
        }
        printf("%d\n", ans);
    }

    return 0;
}


Dinic 算法


/*************************************************************************
	> Name: ./test2.cpp
	> Created Time: 2014年08月10日 星期日 13时42分18秒
 ************************************************************************/

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 200 + 20;
int G[maxn][maxn];
int Layer[maxn];
int Visited[maxn];
int n, m;
int st, ed;

bool CountLayer() {
    int layer = 0;
    deque<int> Q;
    memset(Layer, -1, sizeof(Layer));
    Layer[st] = 0;
    Q.push_back(st);
    while(!Q.empty()) {
        int v = Q.front();
        Q.pop_front();
        for(int i=1; i<=ed; i++) if(G[v][i] > 0 && Layer[i] == -1) {
            Layer[i] = Layer[v] + 1;
            if(i == ed) return true;
            else Q.push_back(i);
        }
    }
    return false;
}

int Dinic() {
    int maxFlow = 0;
    int minFlow = 0;
    deque<int> stac;
    while(CountLayer()) {
        stac.push_back(st);
        memset(Visited, 0, sizeof(Visited));
        Visited[st] = 1;
        while(!stac.empty()) {
            int nd = stac.back();
            if(nd == ed) {
                int minC = INF;
                int minC_vs;
                for(int i=1; i<stac.size(); i++) {
                    int vs = stac[i-1];
                    int ve = stac[i];
                    if(G[vs][ve] > 0 && G[vs][ve] < minC) {
                        minC = G[vs][ve];
                        minC_vs = vs;
                    }
                }
                maxFlow += minC;
                for(int i=1; i<stac.size(); i++) {
                    int vs = stac[i-1];
                    int ve = stac[i];
                    G[vs][ve] -= minC;
                    G[ve][vs] += minC;
                }
                while(!stac.empty() && stac.back() != minC_vs) {
                    Visited[stac.back()] = 0;
                    stac.pop_back();
                }
            } else {
                int i;
                for(i=1; i<=ed; i++) {
                    if(G[nd][i] > 0 && Layer[i] == Layer[nd]+1 && !Visited[i]) {
                        Visited[i] = 1;
                        stac.push_back(i);
                        break;
                    }
                }
                if(i > ed) {
                    stac.pop_back();
                }
            }
        }
    }
    return maxFlow;
}

int main() {

    while(scanf("%d%d", &m, &n) != EOF) {
        memset(G, 0, sizeof(G));
        for(int i=0; i<m; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            G[u][v] += w;
        }
        st = 1, ed = n;
        int ans = Dinic();
        printf("%d\n", ans);
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值