[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的最大流 思路: 传送门:最大流的增广路算法 ...
随机推荐
- 19.SpringCloud实战项目-SpringCloud整合Alibaba-Nacos配置中心
SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...
- 详解 Map集合
(请关注 本人"集合总集篇"博文--<详解 集合框架>) 首先,本人来讲解下 Map集合 的特点: Map集合 的特点: 特点: 通过 键 映射到 值的对象 一个 映射 ...
- 初始化 RESTful API 风格的博客系统
作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在 HelloDjango 全栈系列教程的第一步--Django博客教程(第二版)中 ...
- Stream—一个早产的婴儿
当你会关注这篇文章时,那么意味着你对Stream或多或少有些了解,甚至你在许多业务中有所应用.正如你所知,业界对Stream.lambda褒贬不一,有人认为它是银弹,也有人认为其降低了代码的可读性.事 ...
- C# WCF 之优势及特性
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...
- Python爬虫篇(代理IP)--lizaza.cn
在做网络爬虫的过程中经常会遇到请求次数过多无法访问的现象,这种情况下就可以使用代理IP来解决.但是网上的代理IP要么收费,要么没有API接口.秉着能省则省的原则,自己创建一个代理IP库. 废话不多说, ...
- TIME_WAIT的危害与避免
time-wait的产生: 在TCP连接中四次挥手关闭连接时,主动关闭连接的一方(上图中时Client)会在发送最后一条ACK报文后维持一段时长2MSL(MSL指的是数据包在网络中的最大生存时间)的等 ...
- 2019-2020-1 20199329 第二周测试(环境:ubuntu64位)
2019-2020-1 20199329 第二周测试(环境:ubuntu64位) 实验一 0.每个.c一个文件,每个.h一个文件,文件名中最好有自己的学号 1.用Vi输入图中代码,并用gcc编译通过 ...
- Linux系统管理第六次作业 进程和计划任务管理
1.通过ps命令的两种选项形式查看进程信息 [root@localhost ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STA ...
- Django中的content_type表
models.py from django.db import models from django.contrib.contenttypes.models import ContentType # ...