题目链接:

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---最大流模板题的更多相关文章

  1. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  2. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

  3. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  4. hdu 1532 Drainage Ditches (最大流)

    最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...

  5. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

  6. HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...

  7. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53640   Accepted: 2044 ...

  8. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  10. HDU 1532 Drainage Ditches (网络流)

    A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. Docker:容器与主机时间不同步问题解决

    在Docker容器运行后,可能会发现容器时间与宿主机时间不一致,一般会差8个小时.这样会造成在容器中运行的web程序打出的日志时间与实际时间不一致,如果web程序中有定时任务也会造成影响等,需要对宿主 ...

  2. [poj 1837] Balance dp

    Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...

  3. 2017-10-20 NOIP模拟赛2

    P98 a [问题描述]你是能看到第一题的 friends 呢.——hja世界上没有什么比卖的这么贵的弹丸三还令人绝望的事了,所以便有了这么一道题.定义?(?)为满足(? × ?)|?的有序正整数对( ...

  4. [Xcode 实际操作]三、视图控制器-(6)UINavigationController导航栏样式

    目录:[Swift]Xcode实际操作 本文将演示对导航栏进行样式设置,以及更改导航顶部的提示区. 选择编辑第一个视图控制器文件. import UIKit class FirstSubViewCon ...

  5. Clean Code之JavaScript代码示例

    译者按: 简洁的代码可以避免写出过多的BUG. 原文: JavaScript Clean Code - Best Practices 译者: Fundebug 本文采用意译,版权归原作者所有 引文 作 ...

  6. 帝都Day4(1)——还是dp

    其实是day4 一.洛谷P1018 乘积最大 f[i][j]表示前i个数 切成j块 用f[i][j]而不用f[i][j][k](i到j切成k块)呢? Luogu1043 前缀和(好算一段里的数的和)+ ...

  7. before和after兼容性测试

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. iconfont的应用

    http://www.iconfont.cn/help/platform.html http://www.iconfont.cn/users/project 点击下载之后: 打开demo.html: ...

  9. centos7.3下配置本地yum仓库

    部署openstack时,网络yum源安装慢,而且经常中的提示找不到mirrors,所以配置一个本地的安装源很有必要,来解决这个揪心的问题. 安装:yum install yum-utils crea ...

  10. Ubuntu下rsyslog集中收集mysql审计日志

    服务端 1.安装最新版本rsyslog sudo apt-get install software-properties-common python-software-properties sudo ...