[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;} |
[hdu1532]最大流的更多相关文章
- HDU1532最大流 Edmonds-Karp,Dinic算法 模板
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu1532 最大流板子题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目给出源点和漏点,还有一些边,要求源与漏之间的最大流,我采用了Edmonds Karp算法,该 ...
- hdu1532 (最大流入门,EK算法)
看着这个博客 然后敲了hdu1532这个入门题,算是对最大流有点理解了 #include <stdio.h> #include <string.h> #include < ...
- 【最大流之EdmondsKarp算法】【HDU1532】模板题
题意:裸的最大流,什么是最大流,参考别的博客 运用复杂度最高的EK算法 O(M*N),模板来自紫书 #include <cstdio> #include <cstdlib> # ...
- HDU1532 网络流最大流【EK算法】(模板题)
<题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...
- 【最大流之ek算法】HDU1532 求最大流
本来是继续加强最短路的训练,但是遇到了一个最短路 + 最大流的问题,最大流什么鬼,昨天+今天学习了一下,应该对ek算法有所了解,凭借学习后的印象,自己完成并ac了这个最大流的模板题 题目大意:都是图论 ...
- HDU-1532 Drainage Ditches (最大流,EK算法模板)
题目大意:最大流的模板题...源点是0,汇点是n-1. 代码如下: # include<iostream> # include<cstdio> # include<cma ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu-1532 Drainage Ditches---最大流模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...
随机推荐
- MVC-路由扩展-限制浏览器
根据路由原理,MVC每次都会走获取路由上下文数据. 自定义Route 调用,以及完善其他代码 运行结果,当在谷浏览器执行时:
- tp5 -- join注意事项
使用数据库关联查询的时候,有时候会避免不了两个表格字段名称都一样的尴尬, 这时候管理查询出来的只有其中一个表格字段名称的数据,因为在相同字段名称的情况下,数据会自动覆盖. 这时候,我们只需要给其中一个 ...
- Task Scheduler API Error 80041318
https://stackoverflow.com/questions/42307917/task-scheduler-api-error-80041318/42462235#42462235 Hi ...
- [Windows] Diskpart Scripts and Examples
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/diskpart-scripts-and ...
- OSG加载倾斜摄影数据
目录 1. 概述 2. 实例 2.1. 代码 2.2. 解析 3. 结果 1. 概述 ContextCapture(Smart3D)生成的倾斜摄影模型数据一般都形如如下组织结构: 在Data目录下包含 ...
- 一千行mysql笔记
原文地址:https://shockerli.net/post/1000-line-mysql-note/ /* Windows服务 */ -- 启动MySQL net start mysql -- ...
- mycat入门部署安装
mycat是一种比较简单的中间件产品,可以帮助mysql进行分库,同时统一在一个逻辑库. 硬件环境:系统:centos 7.6数据库版本:5.7.19mycat:1.6..6.1 github上下载m ...
- Eclipse Mac OS 安装 最新版 Subversion插件subclipse
subclipse 目前全部转移到github 官方地址 https://github.com/subclipse/subclipse/wiki Eclipse mac版 安装 最新版svn插件sub ...
- flex学习
今天看视频学习的时候,发现css有一个 flex 的属性,非常的好用,为了让自己能够熟记,特意来分享一下flex的用法. 首先,采用Flex布局的元素,称为Flex容器(flex co ...
- 全栈必备Log日志
Log日志,不论对开发者自身,还是对软件系统乃至产品服务都是非常重要的事情.每个开发者都接触过日志,以至于每个人对日志的了解都会有所不同. 什么是日志 日志是什么呢?老码农看来,日志是带有明确时间标记 ...