hdu-1532 Drainage Ditches---最大流模板题
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1532
题目大意:
给出有向图以及边的最大容量,求从1到n的最大流
思路:
传送门:最大流的增广路算法
直接套用模板,用水流来理解网络流
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = ;
const int INF = 1e9 + ;
struct edge
{
int from, to, cap, flow;//分别是起点,终点,容量,流量
edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){}
};
int n, m;//n为点数,m为边数
vector<edge>e;//保存所有边的信息
vector<int>G[maxn];//邻接表,G[i][j]保存节点i的第j条边在e数组里面的编号
int a[maxn];//每个点目前流经的水量
int p[maxn];//p[i]从原点s到终点t的节点i的前一条边的编号 void init(int n)
{
for(int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));//正向边
e.push_back(edge(v, u, , ));//反向边,容量为0
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
int Maxflow(int s, int t)//起点为s,终点为t
{
int flow = ;
for(;;)
{
memset(a, , sizeof(a));//从原点s开始放水,最初每个点的水量都为0
queue<int>Q;//BFS拓展队列
Q.push(s);
a[s] = INF;//原点的水设置成INF
while(!Q.empty())
{
int x = Q.front();//取出目前水流到的节点
Q.pop();
for(int i = ; i < G[x].size(); i++)//所有邻接节点
{
edge& now = e[G[x][i]];
if(!a[now.to] && now.cap > now.flow)
//a[i]为0表示i点还未流到
//now.cap > now.flow 说明这条路还没流满
//同时满足这两个条件,水流可以流过这条路
{
p[now.to] = G[x][i];//反向记录路径
a[now.to] = min(a[x], now.cap - now.flow);
//流到下一点的水量为上一点的水量或者路径上还可以流的最大流量,这两者取最小值
Q.push(now.to);//将下一个节点入队列
}
}
if(a[t])break;//如果已经流到了终点t,退出本次找增广路
}
if(!a[t])break;//如果所有路都已经试过,水不能流到终点,说明已经没有增广路,已经是最大流
for(int u = t; u != s; u = e[p[u]].from)//反向记录路径
{
e[p[u]].flow += a[t];//路径上所有正向边的流量增加流到终点的流量
e[p[u]^].flow -= a[t];//路径上所有反向边的流量减少流到终点的流量
}
flow += a[t];//最大流加上本次流到终点的流量
}
return flow;
}
int main()
{
int M, N;
while(cin >> M >> N)
{
n = N;
int u, v, c;
init(n);
for(int i = ; i < M; i++)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u, v, c);
}
cout<<Maxflow(, n)<<endl;
}
return ;
}
hdu-1532 Drainage Ditches---最大流模板题的更多相关文章
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- hdu 1532 Drainage Ditches (最大流)
最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...
- HDU 1532 Drainage Ditches(最大流 EK算法)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
- HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- 洛谷P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...
- Linux调优(网络)
定义socket接受缓冲大小 net.core.rmem_default = N #接受 net.core.rmem_max = N net.core.wmem_default = N #发送 net ...
- SCOJ4427 / TOPOI 4404: Miss Zhao's Graph 解题报告
题目链接 SCOJ TOPOI 题目描述 Problem 给定一个包含n个顶点m条边的带权有向图,找一条边数最多的路径,且路径上的边的权值严格递增.图中可能有重边和自环. Input Data 第一行 ...
- 洛谷 P1365 WJMZBMR打osu! / Easy
题目背景 原 维护队列 参见P1903 题目描述 某一天\(WJMZBMR\)在打\(osu~~~\)但是他太弱逼了,有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有\(n\)次点击要做,成功 ...
- mac安装scrapy
Mac自带python2.7,所以直接安装scrapy.默认安装了Xcode总共分以下几步:1.安装 homebrew.wget2.安装pip3.安装scrapy 安装homebrew在termina ...
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- haoi2018奇怪的背包题解
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5302 对于一个物品,设它体积为v,那么,在背包参数为p的情况下,它能达到gcd(v,p ...
- OkHttp工具类
package test; import java.io.File; import java.io.IOException; import java.util.ArrayList; import ja ...
- pygame 使用
模块概况 display image event key mouse font 类概况 Rect: 返回的矩阵区域(图片) Surface: 可以看做是一个贴图, 它就是来显示的 display(与显 ...
- sql server数据类型char和nchar,varchar和nvarchar,text和ntext
varchar和nvarchar的区别: varchar(n)长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节 ...