HDU1532 Drainage Ditches —— 最大流(sap算法)

[译]网络流Dinic算法-最大流-HDU1532&POJ1273 POJ1273 HDU1532 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51941   Accepted: 19732 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就 阅读详情

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532


Drainage Ditches

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


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




代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int MAXN = 500+10;

struct Edge
{
    int to, next, cap, flow;
}edge[MAXN*MAXN];
int tot, head[MAXN];

int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];

void add(int u, int v, int w)
{
    edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
    edge[tot].next = head[u]; head[u] = tot++;
    
    edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
    edge[tot].next = head[v]; head[v] = tot++;
}

int sap(int start, int end, int n)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u = start;
    pre[u] = -1;
    gap[0] = n;
    int maxflow = 0;
    while(dep[start]<n)
    {
        bool flag = false;
        for(int i = cur[u]; i!=-1; i=edge[i].next)
        {
            int v = edge[i].to;
            if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i;
                u = v;
                break;
            }
        }

        if(flag)
        {
            if(u==end)
            {
                int minn = INF;
                for(int i = pre[u]; i!=-1; i=pre[edge[i^1].to])
                    if(minn>edge[i].cap-edge[i].flow)
                        minn = edge[i].cap-edge[i].flow;
                for(int i = pre[u]; i!=-1; i=pre[edge[i^1].to])
                {
                    edge[i].flow += minn;
                    edge[i^1].flow -= minn;
                }
                u = start;
                maxflow += minn;
            }
        }

        else
        {
            int minn = n;
            for(int i = head[u]; i!=-1; i=edge[i].next)
                if(edge[i].cap-edge[i].flow && dep[edge[i].to]<minn)
                {
                    minn = dep[edge[i].to];
                    cur[u] = i;
                }
            gap[dep[u]]--;
            if(gap[dep[u]]==0) break;
            dep[u] = minn+1;
            gap[dep[u]]++;
            if(u!=start) u = edge[pre[u]^1].to;
        }
    }
    return maxflow;
}

int main()
{
    int n, m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        tot = 0;
        memset(head,-1,sizeof(head));
        for(int i = 1; i<=m; i++)
        {
            int u, v, c;
            scanf("%d%d%d",&u,&v,&c);
            add(u, v, c);
        }
        cout<< sap(1, n, n) <<endl;
    }
}


回归预测 | MATLAB实现SVR(支持向量机回归)K折交叉验证 回归预测 | MATLAB实现SVR(支持向量机回归)K折交叉验证 目录回归预测 | MATLAB实现SVR(支持向量机回归)K折交叉验证基本介绍划分原理留一划分交叉验证K折交叉数据下载程序设计参考资料致谢 基本介绍 在机器学习里,通常来说我们不能将全部用于数据训练模型,否则我们将没有数据集对该模型进行验证,从而评估我们的模型的预测效果。为了解决这一问题,有如下常用的方法: 第一种是最简单的,也是很容易就想到的。我们可以把整个数据集分成两部分,一部分用于训练,一部分用于验证,这也就是我们经常提到的训练集 阅读详情

相关推荐

自建邮局服务器| 批量邮件群发系统PowerMTA+oem(mumara,mailwizz, interspire)

世界顶级自建邮局服务器PowerMTA+oem(mumara,mailwizz, interspire) PowerMTA4.5(最好用的 群发邮件软件)是用于大批量多ip开发的邮件传递邮寄。可按每小时发送数近百万条电子邮件,发送IP地址和域可以详细配置,加上目前流行前端oem,mumara,mailwizz, interspire等,可定时发送,定时重复发送,实现全自动化 邮件群发 方案。 官网(powermta4.5下载) https://www.sparkpost.com/powermta/ 使用Po

ytrsexpo的博客 6503

HDU1532 Drainage Ditches 网络流之SAP算法

HDU1532:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19000 Accepted Submission(s): 9058 Problem Description Every time it rain

雪的期许 960

【Verilog模块例化全解析:从入门到精通】

本文详细解析了Verilog模块例化的核心概念与方法。主要内容包括: 模块定义与例化的基本概念,强调模块例化会生成独立硬件电路; 两种端口连接方式:位置映射(按顺序连接)和名称映射(按端口名连接),推荐使用名称映射提高可读性; 参数化例化方法,通过parameter实现可配置模块设计; 多层模块嵌套的实战应用,展示复杂设计的层次化实现。 文章通过2位加法器等代码示例,对比分析了不同例化方式的优缺点,为硬件设计中的模块复用提供了实用指导。重点推荐名称映射和参数化设计方法,以提高代码可维护性和设计灵活性。

m0_58476824的博客 1328

hdu1532 sap模板

之前在做好多题 都用dicnic和E-K算法  结果连续T了好几天一道题也没过 直到知道还有SAP算法 就直接摘了kuangbin的模板 在网上找了好多的模板 发现还是kuangbin的最好用 用裸题hdu1532上代码 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory

Frozensmile的博客 518

Rust 与 C/C++:Rust 比 C/C++ 更好还是只是“技能问题”?

对于许多嵌入式系统开发人员而言,的崛起似乎掀起了一股不小的浪潮,特别是在相较于传统的语言,Rust的“内存安全”优势让人眼前一亮。然而,是否可以将Rust作为C/C++的替代品?或者说,Rust是否真的比C/C++更好,抑或它只是一个“技能问题”呢?在嵌入式开发的领域,我观察到,尽管Rust在一些高性能、内存安全敏感的场景中受到了广泛推崇,但它在实际应用中的普及度,特别是在嵌入式领域,依然有限。今天,我会从多个角度探讨这个问题,并且分享一些对于Rust、C/C++以及嵌入式系统开发的个人见解。

qq_63315166的博客 1300

S32K144 LPI2C Master/Slave模式配置与多设备通信实战

本文详细介绍了NXP S32K144微控制器中LPI2C模块的主从模式配置与多设备通信实战。内容涵盖硬件连接、引脚配置、主从模式深度设置、中断/DMA应用,并通过AT24C16 EEPROM、SHT30传感器等实例,解析了多设备通信中的地址切换、时序控制及常见问题排查技巧,帮助开发者高效实现基于LPI2C的稳定通信。

weixin_29158047的博客 269

hdu1532最大流EK与SAP算法

EK是最朴素的最大流算法了,但效率比较慢,当然代码也比较清晰,思路去看LRJ的白书就行了。 但是由于网上大牛总结SAP基本上是解决最大流的最标准模板了,  网上大牛说基本没有什么最大流能卡SAP。   EK代码: #include #include #include using namespace std; const int N = 210; //小规模 不用考虑效率

amourjun的专栏 1176

服务器基础知识介绍

服务器,不仅仅是一台高性能计算机,更是网络的核心节点,承担着网络服务的责任。然而,相较于普通计算机,服务器对于稳定性、安全性和性能等方面提出了更高的要求,这使得它们在数字化浪潮中傲然独立。在硬件成本结构中,CPU和芯片组占据约50%的成本,内存占15%,外部存储占10%,其余硬件约占25%。服务器的逻辑结构与普通计算机相似,但由于其需要提供高性能计算,对稳定性、可靠性、安全性、可扩展性和可管理性的要求更高。服务器的分类多种多样,根据产品形式可分为塔式服务器、机架服务器、刀片服务器和机柜服务器。

m0_73576645的博客 1882

【最大流之sap】【HDU1532】模板题

与上题一样 纯属测试模板 来自kuangbin的模板 #include #include #include #include #include #include #include #include #include #define oo 0x13131313 using namespace std; const int MAXN=200+5; const int MA

学习之路 863

一个测试工程师走进一家酒吧……

一个测试工程师走进一家酒吧,要了一杯啤酒;一个测试工程师走进一家酒吧,要了一杯咖啡;一个测试工程师走进一家酒吧,要了 0.7 杯啤酒;一个测试工程师走进一家酒吧,要了-1 杯啤酒;一个测试...

字节跳动技术团队官方博客 4886

2026年摩尔线程数字IC面试试卷带答案

本文摘要: 该试卷为数字IC设计笔试,满分120分,时长90分钟,包含单选题、波形分析题、多选题和代码设计题。重点考察时序约束(如set_false_path)、建立/保持时间、逻辑化简、复位机制、AXI协议、半导体工艺、状态机、GPU架构等核心知识点。多选题涉及异步FIFO设计、Verilog综合、亚稳态处理、C++关键字和低功耗技术。代码题要求实现单比特信号去毛刺电路,需稳定4个时钟周期才输出。试题解析详细,标注了企业面试高频考点,如摩尔线程自研的MUSA架构和花港GPU架构特点。整体内容专业性强,覆盖

Open 98

最大流自用模板(例题:HDU1532)

三种模板:Edmonds_Karp,Dinic,SAP 例题: Drainage Ditches(HDU1532) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22365    Accepted Submission(s): 1...

Radium_1209 219

采用了fluent模拟了平壁面结霜过程。udf,很经典,可以试试。

利用该模型作为FLUENT中的源项,模拟了在局部冷却下冷表面上的霜形成,以预测霜的形态、温度分布和霜的重量,结果与实验结果吻合良好。结果显示,霜首先围绕冷却块形成,然后逐渐向其他方向扩展,冷却块上方的霜密度最大。随着时间的推移,平均霜厚度逐渐增加,生长速率减慢,而平均霜密度随增长速率变快而增加。相变传递微观过程与机理、蒸发及冷凝换热、强化传热、高效传热管、热泵、制冷、节水节能新技术、电子元器件冷却、余热回收及梯级利用等。udf,很经典,可以试试。工程热物理、空调、热能、低温制冷、化工、物理等。

qq_15963745的博客 922

网络流改进SAP算法模版、HDU 1532 Drainage Ditches(解题报告)

转载请注明出处:http://write.blog.youkuaiyun.com/postlist 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 本次网络流算法看了两天了,先学习了EK算法,发现速度不够快,于是查找资料,得网络流诸多算法中主流算法(SAP算法)学习之。 相关链接: 1、网络流的算法分类:http://www.notonl

ACM菜鸟的点滴_CXB 5492

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

HDU1532 Drainage Ditches SAP+链式前向星

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17065Accepted Submission(s): 8066 Problem Description Eve...

ddqb3235的博客 143

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

HDU1532 Drainage Ditches 网络流--dinic算法 SAP算法

HDU1532:http://acm.hdu.edu.cn/showproblem.php?pid=1532Drainage DitchesTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19000    Accepted Submissi...

雪的期许 1078

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

HDU1532 Drainage Ditches(最大流)

学了一下午网络流,AC了这道模板题 勉强算是自己敲的,debug时参照了下模板,模板参照kuangbin大神 邻接表+isap算法 #include #include #define MAXN 210 #define MAXM 420 #define INF 0x3f3f3f3f struct node{ int to,next,cap; }edge[MAXM]; int

丶Hiroshi 587

hdu 1532 Drainage Ditches

点击打开题目链接 裸的最大流问题,用的是SAP算法。 #include #include #include #define clr(a,b) memset(a,b,sizeof(a)) #define M 0xfffff using namespace std; const int I=300; int map[I][I],pre[I],dis[I],gap[I]; int SAP(int

明天,你又将身在何方…… 797

hdu 1532 Drainage Ditches (网络流两种方法)

hdu 1532 Drainage Ditches (网络流两种方法)

3808

Hdu 1532 Drainage Ditches【网络流】

网络流模板提 Sap模板: const int N = 220 , M = 2020; #define inf (1<<29) int gap[N] , dis[N] , pre[N] , cur[N]; int NV , n , m; struct Edge { int v , w , next; Edge () {} Edge(int v,int w,int nex

算法街区 604

nyoj 323 hdu 1532 Drainage Ditches(最大流)

网络流基础网络流(理论详解)网络流(一) 入门到熟练网络流之最大流(FF, EK, Dinic, SAP)#include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;queue&gt; #include &lt;cstring&gt; using namespace std; con...

穆梓先生 225

【网络流第一步】Hdu 1532——Drainage Ditches

题目链接:点击打开链接 看了一会网络流的概念,找了个模板就上了。。起初TLE了几遍,发现是初始化没搞好。。 sap优化据资料说比EK省好多时间的说,网络流的精髓应该是构图,以后碰到难题应该会有所体会的。网络流有三个性质:一个节点流量守恒(形如电路中的KVL/KCL),流量双向,f(i,j) 毕竟第一个网络流模板题的说,直接用SAP了。 #include #include #includ

圣人不仁,使草胜豆苗稀。 967

【网络流第二步】Hdu 1532——Drainage Ditches 邻接矩阵版

不多说,HDU卡数据还是挺厉害的 #include #include #include #include #include #include #include #define maxn 205 #define INF 100000000 using namespace std; //Template Bade By: Xiaobin Chen int pointnum,

圣人不仁,使草胜豆苗稀。 761

HDU-1532 Drainage Ditches,人生第一道网络流!

Drainage Ditches    自己拉的专题里面没有这题,网上找博客学习网络流的时候看到闯亮学长的博客然后看到这个网络流入门题!随手一敲WA了几发看讨论区才发现坑点!    本题采用的是Edmonds-Karp算法求增广路。小白书上只介绍了这个算法,确实对于数据不刁钻的题目这个算法足以应对。大白书上的Dinic及SAP、ISAP还没有去看,以后慢慢攻克吧!    回到这个题:n条水渠

NYIST_TC_LYQ的博客 665
上一篇: HDU3394 Railway —— 点双联通分量 + 桥
下一篇: POJ2135 Farm Tour —— 最小费用最大流
DOLFAMINGO
博客等级 码龄10年 8粉丝 216原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值