HDU1532——Drainage Ditches(网络流Dinic算法)

本文通过Dinic算法解决排水沟渠问题,确保雨水不会淹没Bessie喜爱的草地。介绍了如何构建地图并进行节点分层,通过寻找路径上的最小容量来调整水流速率,最终求得最大排水率。

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

Drainage Ditches

点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1532

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14961    Accepted Submission(s): 7109


Problem 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
解题思路:
这道题用EK或者Dinic算法都可以,这里小龙人用的是Dinic算法。
首先建造地图给每个节点分层(比如 1->2, 2->4, 1->3, 3->4, 2->3 这个图,1是0层,2,3是1层,4是2层),每个节点只能走向下一层的节点,并且这两个节点有水管。分层分好后,比如有两条路,依次走这两条路,选出每条路最小的水管容量,然后用这条路所有水管容量依次减去最小水管容量,并且添加反向容量(反向容量=该水管的流量,至于为什么要添加,这里比较难懂,小龙人建议去问问身边的大神),返回最小水管容量,两条路走完后,再次给地图节点分层(这时,流量达到容量的水管已经为0,或者说这条路已经不通了),重复上面的步骤,最后直到分层后不能给汇点分层就结束。
代码实现:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <queue>
#include <vector>

using namespace std;
const int maxn = 205;
const int MAXN = ( 1<<29 );
int m, n;
int mp[maxn][maxn];
int dis[maxn];

bool bfs( int d )                               //用bfs给图分层;
{
    queue<int>q;
    memset(dis, -1, sizeof(dis));
    dis[d] = 0;
    q.push(d);
    while( !q.empty() )
    {
        int top = q.front();
        q.pop();
        for( int j=1; j<=n; j++ )
        {
            if( dis[j] == -1 && mp[top][j] > 0 )
            {
                dis[j] = dis[top] + 1;           //层数+1;
                q.push(j);
            }
        }
    }
    if( dis[n] > 0 )                             //直到汇点不能分层结束;
        return true;
    return false;
}

int findd( int k, int low )
{
    if( k == n )
        return low;
    int a = 0;
    for( int i=1; i<=n; i++ )
    {
        if( dis[i] == dis[k]+1
           && mp[k][i] > 0
           && ( a = findd( i, min(low, mp[k][i]) )) )     // 用递归找到该路的最小容量;
        {
            mp[k][i] -= a;                                // 该路各水管依次减去最小容量;
            mp[i][k] += a;                                // 添加反向容量;
            return a;
        }
    }
    return 0;
}

int main()
{
    while( scanf("%d%d",&m,&n)!=EOF )
    {
        memset(mp, 0, sizeof(mp));
        int si, ei, ci;
        for( int i=0; i<m; i++ )
        {
            scanf("%d%d%d",&si,&ei,&ci);
            mp[si][ei] += ci;                 // 细节注意,这里必须用叠加,有可能 1->2有两根水管叠加;
        }
        int ans = 0, temp;
        while(bfs(1))
        {
            while( temp = findd( 1, MAXN ) )
                ans += temp;
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值