HDU 1532 Drainage Ditches(最大流+EK算法模板题)

本文介绍了一个典型的最大流问题案例,通过构建复杂网络来确定水源从池塘流向溪流的最大速率。使用了Edmonds-Karp算法模板进行求解,并提供了一份完整的C++实现代码。

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

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

题解:

源点为1,汇点为n,给num个流,让你求最大流。。。EK模板题目,虽然网络流刚刚入门的我不太懂但是直接按着紫书上的模板打了一遍,修改了些就ac了

模板会用就行了qwq

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
#define INF 100861111
#define ll long long
#define eps 1e-5
#define maxn 205
struct edge//存边
{
    int from,to,cap,flow;//cap为容量,flow为流量
    edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<edge>ed;
vector<int>G[maxn];//存第i个点连接的第j条边的标号
int a[maxn];//存起点到i的可改进量。。不太懂,照着紫书打的
int p[maxn];//最短路树上p的入弧编号
int m,n;
void init()
{
    for(int i=1;i<=n;i++)
        G[i].clear();
    ed.clear();
}
void addEdge(int from,int to,int cap)
{
    ed.push_back(edge(from,to,cap,0));
    ed.push_back(edge(to,from,0,0));//反向加边
    m=ed.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
int Maxflow(int s,int t)//s为源点,t为汇点
{
    int flow=0;
    while(1)
    {
        memset(a,0,sizeof(a));
        queue<int>q;
        q.push(s);
        a[s]=INF;
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;i<G[x].size();i++)
            {
                edge &e=ed[G[x][i]];
                if(!a[e.to]&&e.cap>e.flow)
                {
                    p[e.to]=G[x][i];
                    a[e.to]=min(a[x],e.cap-e.flow);
                    q.push(e.to);
                }
            }
            if(a[t])
                break;
        }
        if(!a[t])
            break;
        for(int e=t;e!=s;e=ed[p[e]].from)
        {
            ed[p[e]].flow+=a[t];
            ed[p[e]^1].flow-=a[t];
        }
        flow+=a[t];
    }
    return flow;
}
int main()
{
    int i,j,x,y,c,num;
    while(scanf("%d%d",&num,&n)!=EOF)
    {
        init();
        for(i=0;i<num;i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            addEdge(x,y,c);
        }
        printf("%d\n",Maxflow(1,n));
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值