HDU 1532 Drainage Ditches (最大流模板)

STM32G0实战:HAL库与LL库Flash操作避坑指南(附HardFault解决方案) 本文深入剖析了STM32G0系列微控制器在Flash操作中常见的HardFault问题,对比了HAL库与LL库在读写擦除操作上的底层差异与潜在陷阱。文章提供了基于寄存器状态分析的精准调试方法,并给出了经过实战检验的解决方案与健壮的代码框架,帮助开发者有效规避数据丢失与系统崩溃风险。 阅读详情

Drainage Ditches

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


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
 

Source
 

思路:这是最大流模板,用的是Ford-Fulkerson算法,大概意思就是一副有向图,有一个源点,一个汇点,每条边都有最大承载容量,源点可以发出任意大小的流量,求汇点接收流量的最大值。源点发出 的流量一定等于汇点接收的流量( f(s) = f(t) ),为了求解方便,将有向图改成无向图,单向边都加上另一个方向的边,并且都求得该方向的容量,反向边的流量其实相当于将正向边的流量推回发出流量的节点并用于别的路径。有了双向边之后,无向图就建立成功了,要进行dfs,每次dfs都是寻找增广路,就是找到一条由全都满足容量大于0(正向是f(e) < c(e),反向则是f(e)>0)的边的路,并根据最后的边来确定这条增广路能增加多少流量。整个过程就是不停的寻找增广路增加流量,直到找不到增广路,就求出了最大流量。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#define MAXV 205
#define INF 0x3f3f3f3f 
using namespace std;
struct edge
{
	int to, cap, rev;//边另一边的顶点,该边该方向的容量,反向边在数组g[to]的下标i 
};
vector<edge> g[MAXV];
bool used[MAXV];
int m, n;
void add_edge(int from, int to, int cap)
{
	g[from].push_back((edge){to, cap, g[to].size()});//注意这里的size不用减一
	g[to].push_back((edge){from, 0, g[from].size() - 1});//因为下面即将添加 
}
int dfs(int s, int t, int f)
{
	if(s == t)return f;//如果已经到终点了,则返回到达终点的流量。 
	used[s] = 1;
	for(int i = 0; i < g[s].size(); i++)
	{
		edge &e = g[s][i];//引用,类似于指针,改变了e就改变了g[s][i] 
		if(!used[e.to] && e.cap > 0)//若该边访问过且该边该方向容量有余 
		{
			int d = dfs(e.to, t, min(f,e.cap));//递归,流量取该边容量和该点拥有流量的最小值 
			if(d > 0)
			{
				e.cap -= d;//正向边容量减去d 
				g[e.to][e.rev].cap += d;//反向边容量加上d,相当于可以推回节点的流量增加了d 
				return d;
			}
		}
	}
	return 0;
}
int max_flow(int s, int t)//求s到t的最大流 
{
	int flow = 0;
	while(1)
	{
		memset(used, 0, sizeof(used));//每次dfs都要初始化 
		int f = dfs(s, t, INF);//源点的流量为无穷大 
		cout<<f<<endl; 
		if(f == 0) return flow;//如果无法找到增广路了,就说明flow已经为最大流 
		flow += f;
	}
}
int main()
{
	int i, j, u, v, c;
	while(scanf("%d%d", &m, &n) != EOF)
	{
		for(i = 1; i <= n; i++)//初始化,清空邻接表 
		{
			g[i].clear();
		}
		for(i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &u, &v, &c);
			add_edge(u, v, c);//双向添加边 
		}
		printf("%d\n",max_flow(1, n));
	}
	return 0;
} 

        

【产品经理修炼之道】-如何搭建企业级账务系统? 笔者公司的业务模式具有一定的独特性,不同于我们常见的市面上供应链类公司的进销存模式。公司主要为各大银行和金融机构提供信贷领域的风控服务,客户主要通过请求公司的数据产品接口产生费用,可以简单地理解为请求一次接口收费0.5元,一个月若是有1w人请求同一个接口则公司需要收费5000元。 阅读详情

相关推荐

深入解析摄像头硬件接口:DVP、MIPI-CSI2与USB的技术对比与应用场景

本文深入解析了摄像头三大主流硬件接口DVP、MIPI-CSI2与USB的核心技术差异。DVP接口作为并行传输的“老兵”,成本低但带宽和抗干扰能力有限;MIPI-CSI2凭借差分串行设计成为移动高清设备的首选,具备高带宽和强抗干扰性;USB接口则依托UVC标准实现了跨平台的即插即用。文章通过对比传输方式、带宽、应用场景等关键维度,为智能硬件、嵌入式系统等领域的工程师提供了清晰的选择指南。

c7d8e的博客 520

【题解】Drainage Ditches HDU - 1532(最大流 模板题)⭐⭐

Drainage Ditches HDU - 1532 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 ...

Suprit's blog 630

QT实现HTTP文件(夹)下载的完整流程与实战技巧

本文详细介绍了使用QT框架实现HTTP文件和文件夹下载的完整流程与实战技巧。通过QNetworkAccessManager等核心类,讲解了从单文件下载、大文件处理、进度显示、暂停/取消功能,到复杂的文件夹下载策略。文章重点剖析了断点续传、错误处理与网络健壮性等关键问题,为C++桌面应用集成稳定高效的下载功能提供了专业指导。

weixin_29239279的博客 284

HDU 1532 Drainage Ditches【最大流 | Edmonds-Karp模板】

题目链接 题目描述: 最大流Edmonds-Karp算法模板题。 最大流 最大流问题是基于有向图的,就是求两点 (分别称为源点、汇点) 间的最大流量,图中任何点都可以作为它们的中转。在求最大流时需要满足以下 3 个性质: 流量守恒。从源点 s 流出的流量和到达汇点 t 的流量相等;其他所有中转点,流入和流出相等。 反对称性。设从 uuu 到 vvv 的流量是 f(u,v)f(u,v)f(u,v),vvv 到 uuu 流量是 f(v,u)f(v,u)f(v,u),那么 f(u,v)=−f(v,u)f(u

weixin_44258590的博客 254

Jetson Nano系统重装的艺术:从格式化到烧录的深度解析

本文详细解析了Jetson Nano系统重装的完整流程,从SD卡格式化到系统镜像烧录的进阶技巧,包括硬件选择、软件准备、常见问题处理及深度学习环境配置。特别推荐使用三星EVO Plus SD卡和官方电源以确保稳定性,适合开发者快速掌握嵌入式设备系统重装的核心技术。

weixin_28273593的博客 613

7.29华为OD机试真题 新系统 - 能量对撞 (Java/Py/C/C++/Js/Go)

7.29华为OD机试真题 新系统 - 能量对撞 (Java/Py/C/C++/Js/Go)

算法大师! 1342

HDU1532 Drainage Ditches——最大流(EK、Dinic模板)

点这里 题意: 源点1以及汇点m,n条带权有向边的权值表示每条边的限制容量。求源点到汇点的最大流。 题解: 最大流的相关概念和算法参考最大流问题与Ford-Fulkerson算法介绍、网络流(一) 入门到熟练 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int N = 300; int n, m, pre[N], G[N][N]; //G记录残留网络 int bfs(...

lllllan 191

【深度学习新浪潮】深度学习特别是大模型在图像配准方面的研发进展

深度学习,尤其是大模型的出现,彻底改变了图像配准的技术范式——从“手工设计特征+几何约束”走向“数据驱动+通用特征学习”。当前,大模型配准已在医学影像、遥感、工业检测等领域实现商业化落地,解决了传统方法难以应对的跨模态、少样本、复杂形变等问题。但大模型配准仍面临推理效率、可解释性、数据隐私等挑战,未来的研发核心将是“精度与效率的平衡”“泛化能力的提升”“场景化适配的简化”。对于开发者而言,从简化版大模型配准代码入手,逐步理解预训练+微调的核心逻辑,是衔接前沿技术的有效路径。

专注:太空算力、AI infra、异构调度、大模型成本工程 432

HDU - 1532 Drainage Ditches(最大流模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 来源:hdu   题意:给你一个网络,并且给了最大的流量,求这个网络的最大流。   EK——增广路算法: #include<bits/stdc++.h> using namespace std; const int Max_m=210; const int Max_n=210;...

Sun 242

出现 Transaction rolled back because it has been marked as rollback-only 解决方法

出现 Transaction rolled back because it has been marked as rollback-only 解决方法

码农研究僧的博客 1万+

STM32Cube高效开发教程<基础篇>(一)----概述

一、 STM32系列器件和开发工具发展历史声明:本人水平有限,博客可能存在部分错误的地方,请广大读者谅解并向本人反馈错误。本专栏博客参考《STM32Cube高效开发教程(基础篇)》,有意向的读者可以购买正版书籍辅助学习,本书籍由王维波老师、鄢志丹老师、王钊老师倾力打造,书籍内容干货满满。一、 STM32系列器件和开发工具发展历史。

我的博客 1182

hdu1532Drainage Ditches 最大流模板水题

感觉这道题纯粹就是试模板的,给一个有向图,求其最大流 Dinic算法:#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define inf 300 #define lim 0x7fffffff int flow[inf][inf]; int dis[inf];

ijbuhv的专栏 1400

Autosar MCAL-S32k324 Crypto配置-基于EB

NXP官网提供了免费的HSE固件,同时也提供了对应的协议栈。本文以算法为例,介绍MCAL相关的配置及在代码中的集成与使用。使用HSE计算,比软件算法确实方便许多。后面会完善更多的算法测试。

赞的博客 2465

HDU 1532Drainage Ditches(最大流模板题 ISAP)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11306    Accepted Submission(s): 5328 Problem Description Every time i

青山绿水之辈 专栏 886

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

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 Be

HJY 659

NTscan扫描工具

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

HDU1532 Drainage Ditches 最大流Dinic模板

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16186 Accepted Submission(s): 7695 Problem Description Every time it rains o

LuRiCheng的博客 594

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

HDU 1532 Drainage Ditches 最大流模板题

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题意:给定两个数n m,分别代表图的边数和顶点数,然后n行形式如a b c,代表a到b之间有一条容量为c的有向边。求1到m的最大流 思路:就是一模版题啊。。。 #include #include #include #include #include using nam

弱智就要多努力 515

算法模板之Dinic最大流(HDU 1532 Drainage Ditches)

模板总结归纳://src 源点, sink汇点, g、e存边的邻接表 const int inf = 1000000000; //1e9 const int maxn = 20000, maxm = 500000; struct Egde { int v, f, nxt; }; int n, src, sink; int g[maxn + 10]; int nume; struct Ed...

摸不着脑子的博客 262

Drainage Ditches HDU - 1532 最大流 模板

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, Fa

liluoyu_1016的博客 227

HDU 1532 Drainage Ditches 最大流模板题

第一次做最大流,写了一个。之后发现网上和网上的很多代码都不同。因为暂时看不懂他们写的。所以,在练习一些之后在返回来更新这篇博客。继续加油!!!

ccutyear的博客 426

Drainage Ditches hdu 1532 最大流模板+入门题目

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7563    Accepted Submission(s): 3559 Problem Description Every tim

风华绝代 818

hdu 1532 Drainage Ditches 网络最大流模板 EK + Dinic两种算法AC

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 drainag

Lionel 1003

Drainage Ditches HDU - 1532(EK算法&Dinic算法 最大流模板)

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 Be

JSASL的博客 248

HDU1532_Drainage Ditches_最大流模板题

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17634    Accepted Submission(s): 8295 Problem Description Every ti

弱的奋斗之旅 326

【最大流 模板题 EdmondsKarp】HDU - 1532 Drainage Ditches

Problem Description 给你m,n分别代表m条边,n个点。接下来给你m条边,每条边u,v,w。u->v流量的容量为w。有重边 思路:模板题,所以给几个模板EdmondsKarp-dfs实现//dfs实现 #include<bits/stdc++.h> using namespace std; const int N = 1000; const int inf = 0x3f3f3f

ACMer 梁剑锋 的博客 308

HDU 1532 Drainage Ditches(最大流)

HDU 1532 Drainage Ditches 题目链接 最大流模板题 代码: #include #include #include #include using namespace std; const int MAXNODE = 205; const int MAXEDGE = 405; typedef int Type; const Type INF

Remilia's 666
上一篇: HDU 2586 How far away ? (LCA模板题 Tarjan算法求最小公共祖先)
下一篇: POJ 3164 Command Network (最小树形图模板 朱刘算法)
LiQiyaoo
博客等级 码龄11年 337粉丝 206原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值