POJ1273 Drainage Ditches 【网络流 最大流/最小割 EK/Dimic 】

本文探讨了如何通过构建排水沟渠系统并合理控制水流速率来确保农田不受水淹的问题。利用图论中的最大流算法(如EK算法和Dimic算法),文章详细介绍了如何计算从水源到河流的最大排水速率,并提供了两种不同的算法实现代码。

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

rainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 82312 Accepted: 31968

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

USACO 93

题解代码(EK算法 |多次寻找增广路)

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#define INF 0x7f7f7f7f
int G[205][205];
int pre[205];
int flow[205];
int m,n;
int bfs(int s,int t){
    std::queue<int> q;
    memset(pre,-1,sizeof(pre));
    pre[s] = 0;
    flow[s] = INF;
    q.push(s);
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        if(cur == t) break;
        for(int i=1;i<=n;i++){
            if(pre[i]==-1&&G[cur][i]>0 &&cur!=i){
                pre[i] = cur;
                flow[i] = std::min(flow[cur],G[cur][i]);
                q.push(i);
            }
        }
    }
    if(pre[t] == -1)return -1;
    return flow[t];
}
int EK(int s,int t){
    int delta = 0;
    int total = 0;
    while(1){
        delta = bfs(s,t);
        if(delta == -1) break;
        int cur = t;
        while(cur != s){
            G[cur][pre[cur]] += delta;
            G[pre[cur]][cur] -= delta;
            cur = pre[cur];
        }
        total += delta;
    }
    return total;
}
int main() {
    while(~scanf("%d %d",&m,&n)){
        memset(flow,0,sizeof(flow));
        memset(G,0,sizeof(G));
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
        }
    }
    return 0;
}

题解代码( 链式前向星 | Dimic算法 )

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#define INF 0x7f7f7f7f

int n,m;
int edgeNum;
int head[205];
int depth[205];
typedef struct edge{
    int v,w;
    int next;
}edge;
edge Edge[410];
void addEdge(int u,int v,int w){
    Edge[edgeNum].v = v,Edge[edgeNum].w = w;
    Edge[edgeNum].next = head[u];head[u] = edgeNum++;
    Edge[edgeNum].v = u,Edge[edgeNum].w = 0;
    Edge[edgeNum].next = head[v];head[v] = edgeNum++;
}
void init(){
    edgeNum = 0;
    memset(head,-1,sizeof(head));
    memset(depth,0,sizeof(depth));
    memset(Edge,0,sizeof(Edge));
}
int dfs(int u,int t,int delta){
    if(u == t)return delta;
    for(int i=head[u];i!=-1;i=Edge[i].next){
        int v = Edge[i].v;
        int w = Edge[i].w;
        if(depth[u]+1 == depth[v] && w){
            int temp = dfs(v,t,std::min(w,delta));
            if(temp > 0){
                Edge[i].w -= temp;
                Edge[i^1].w += temp;
                return temp;
            }
        }
    }
    return 0;
}
int bfs(int s,int t){
    memset(depth,0,sizeof(depth));
    depth[1] = 1;
    std::queue<int>q;
    q.push(s);
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i=head[cur];i!=-1;i=Edge[i].next){
            int v = Edge[i].v;
            int w = Edge[i].w;
            if(w && depth[v] == 0){
                depth[v] = depth[cur] + 1;
                q.push(v);
            }
        }
    }
    return depth[t];
}
int dimic(int s,int t){
    int ans = 0;
    while(bfs(1,n)){
        while(int delta = dfs(1,n,INF)){
            ans += delta;
        }
    }
    return ans;
}
int main() {
    while(~scanf("%d %d",&m,&n)){
        init();
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            addEdge(u,v,w);
        }
        printf("%d\n",dimic(1,n));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值