[hdu1532]最大流
裸最大流,求最大流一般步骤如下:
(1)所有正向边权初始化为容量,反向边权初始化为0
(2)找增广路
(3)找到则进入(4),否则得到最大流并退出
(4) 增广路上所有边减去最小边权,相应的方向边加上最小边权,然后返回(2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <ctime>
#include <deque>
#include <queue>
using namespace std;
struct MaxFlow {
private :
const static int maxn = 2e2 + 7;
struct Edge {
int u, v, w;
Edge( int u = 0, int v = 0, int w = 0): u(u), v(v), w(w) {}
};
vector<vector< int > > G;
vector<Edge> E;
int S, T, maxflow;
bool vis[maxn];
int Q[maxn], fa[maxn], Y[maxn], head, tail, flow[maxn];
public :
void init( int s, int t, int n) {
G.clear();
G.resize(n + 2);
E.clear();
S = s;
T = t;
maxflow = 0;
}
void add( int u, int v, int w) {
E.push_back(Edge(u, v, w));
E.push_back(Edge(v, u, 0));
int sz = E.size();
G[u].push_back(sz - 2);
G[v].push_back(sz - 1);
}
bool bfs( int src) {
head = tail = 0;
memset (vis, 0, sizeof (vis));
Q[tail ++] = src;
flow[0] = 0x7fffffff;
vis[src] = true ;
while (head < tail) {
int node = Q[head ++];
if (node == T) {
maxflow += flow[head - 1];
int p = head - 1;
while (p) {
E[Y[p]].w -= flow[head - 1];
E[Y[p] ^ 1].w += flow[head - 1];
p = fa[p];
}
return true ;
}
for ( int i = 0; i < G[node].size(); i ++) {
int e = G[node][i];
if (!vis[E[e].v] && E[e].w) {
vis[E[e].v] = true ;
fa[tail] = head - 1;
Y[tail] = e;
flow[tail] = min(flow[head - 1], E[e].w);
Q[tail ++] = E[e].v;
}
}
}
return false ;
}
int solve() {
while (bfs(S));
return maxflow;
}
} ;
MaxFlow solver;
int main() {
#ifndef ONLINE_JUDGE
freopen ( "in.txt" , "r" , stdin);
#endif // ONLINE_JUDGE
int n, m;
while (cin >> m >> n) {
solver.init(1, n, n);
for ( int i = 0; i < m; i ++) {
int u, v, w;
scanf ( "%d%d%d" , &u, &v, &w);
solver.add(u, v, w);
}
cout << solver.solve() << endl;
}
return 0;
}
|
转载于:https://www.cnblogs.com/jklongint/p/4675178.html
[hdu1532]最大流相关推荐
- hdu1532(最大流裸题)
题意: 裸的最大流. 思路: 先试了一下红书模板,TLE了,想起来那个比较快的模板,直接就过了...果然模板的质量也是不一样的. 代码: #include<cstdio> #include ...
- 网络流入门——算法模板,习题和解析
最近一段时间再搞网络流,今天终于搞完了!!!!QAQ,好累呀. 首先是关于网络流的基础知识,这部分东西有点多,就不在这里说了,给几个有用的资源. 先推荐一下建图的博客:链式向前星,静态链表和邻接矩阵建 ...
- 最大流自用模板(例题:HDU1532)
三种模板:Edmonds_Karp,Dinic,SAP 例题: Drainage Ditches(HDU1532) Time Limit: 2000/1000 MS (Java/Others) ...
- 【AC梦工厂】最大流hdu1532模版题
问题:调控水流从s点经过许多不同容量的水管到达t点的使得流量最大值的问题 概念解释: G(u,v)代表图中的路 c(u,v)代表路的容量 f(u,v)代表的是当前路的流量 r(u,v)代表还能够增加的 ...
- hdu3549(又是最大流模板题)
题意: 裸的最大流模板,比hdu1532多了个case输出... 思路: 还是用hdu1532那个模板... 代码: #include<cstdio> #include<cstrin ...
- Dinic求最大流/最小割
o(v^2*E) 建图时建一条流量为0的反向边,正向边每减去流量f,反向边增加流量f.对于无向图当做两条边. cap:每条边最大流量 建图后: 调用DINIC():用bfs()为每个节点进行层次编号, ...
- stream流对象的理解及使用
我的理解:用stream流式处理数据,将数据用一个一个方法去 . (点,即调用) 得到新的数据结果,可以一步达成. 有多种方式生成 Stream Source: 从 Collection 和数组 Co ...
- 如何判断飞机的年限_技术流带你鉴定前风挡玻璃更换,不再使用日期判断!
这又是一篇关于前风挡玻璃鉴定的文章,我记得在二手车鉴定微信公众号里面已经发布好几篇这样的文章了,当然每篇文章的住重点不同,今天这一篇应该是完结篇,它们在一起能组成一套玻璃更换系列专题课程: 我们回 ...
- SpringCloud Alibaba微服务实战(五) - Sentinel实现限流熔断
什么是Sentinel? 请查看文章:SpringCloud Alibaba微服务实战(一) - 基础环境搭建 构建服务消费者cloud-sentinel进行服务调用 服务创建请查看文章:Spring ...
- 文件流处理流式处理大数据处理
20210315 https://www.yuque.com/7125messi/wydusr/wweetn 42-(重要重要重要)加速pandas(Modin和pandarallel) 重点 htt ...
最新文章
- linux mq发送测试消息,WebSphere MQ测试常用指令
- 10投屏后没有声音_钉钉怎么投屏,秒懂投屏详解
- vuepress build提示YAMLException: end of the stream or a document separator is expected at line 7, colu
- 大数据学情分析_多分学情大数据分析
- Java 面试之线程与锁
- python执行循环内存变大_python – 为什么我的循环在每次迭代时需要更多内存?...
- JDBC衔接DB2、Oracle、MySQL、PostgreSQL
- 文件编码 ANSI、GBK、GB2312、MS936、MS932、SJIS、Windows-31 、EUC-JP 、EBCDIC 等等之间的区别与联系
- SAP BAPI 教程 – 在 ABAP 中创建 BAPI 的分步指南-020
- 管理部门使用计算机属于固定资产核算吗,固定资产核算管理内容
- 华为荣耀20和x10比较_华为畅享20 Pro和荣耀X10哪个好 配置参数谁更胜一筹
- Matplotlib——条形图_3、分组条形图_4、堆叠条形图
- SpringCloud五大神兽之Eureka服务注册(三)——Eureka的自我保护
- Gitee推送本地文件到仓库并且创建子文件夹(详细)
- 【PAT】06 图论
- 物联网毕业设计 单片机火灾报警器设计与实现
- 2022 Robocom世界机器人开发者大赛 CAIP编程赛道 本科组-省赛 挨打记录+题解
- 「 LaTeX 」写论文,调整公式行间距与其中字符的间距
- 分享一套宾馆客房管理系统源码,功能完善,代码完整
- 怎样从PHP文件中提取特征码,关于判断文件唯一性,怎么提取特征码
热门文章
- python xlwings api_python xlwings API接口之NumberFormat用法
- 【渝粤教育】国家开放大学2018年秋季 0365-21T电子商务概论 参考试题
- 操作系统原理(一)操作系统概述和操作系统用户界面
- 23种设计模式(十四)接口隔离之代理模式
- ubuntu16.04下ROS操作系统学习笔记(三 / 一)ROS基础-工作空间
- Linux 系统性能分析工具图解读
- 淺談auto_ptr
- pycharm关闭/开启代码补全/代码提示
- Luogu 2939 [USACO09FEB]改造路Revamping Trails Luogu 4568 [JLOI2011]飞行路线
- LeetCode(15)3Sum