题目链接:

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. Codeforces Round #527 (Div. 3)C(多重集,STRING)

    #include<bits/stdc++.h>using namespace std;const int maxn=1e6+7;pair<string,int>p[maxn]; ...

  2. 对各种lca算法的理解

    1.RMQ+ST 首先注意这个算法的要素:结点编号,dfs序,结点深度. 首先dfs,求出dfs序,同时求出每个结点的深度.然后st算法,维护深度最小的结点编号(dfs序也可以,因为他们俩可以互相转换 ...

  3. MCP|XHK|High-density peptide arrays help to identify linear immunogenic B cell epitopes in individuals naturally exposed to malaria infection(高密度肽段阵列有助于在自然暴露于疟疾感染的个体中识别线性免疫原性B细胞表位)

    文献名:High-density peptide arrays help to identify linear immunogenic B cell epitopes in individuals n ...

  4. Mysql常见问题集锦

    缺少libstdc++.so.6库的原因及解决办法 https://blog.csdn.net/u010417185/article/details/69951312 https://www.cnbl ...

  5. php保存网络图片到本地

    //保存网络图片 function getimg($url) { $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容 if($stat ...

  6. has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

    https://www.cnblogs.com/caimuqing/p/6733405.html // TODO 支持跨域访问 response.setHeader("Access-Cont ...

  7. python入门之paramiko模块

    paramiko用于实现ssh远程连接服务器执行相关操作. paramiko与之依赖模块cryptography最好版本相同,不然可能执行程序会出错. 一.ssh连接服务器执行命令 import pa ...

  8. 强连通图 Tarjan算法

    算法学习:https://blog.csdn.net/qq_16234613/article/details/77431043 http://www.cnblogs.com/chenchengxun/ ...

  9. Gym 101047K Training with Phuket's larvae

    http://codeforces.com/gym/101047/problem/K 题目:给定n<=2000条绳子,要你找出其中三条,围成三角形,并且要使得围成的三角形面积最小 思路: 考虑一 ...

  10. TortoiseSVN 控制图标未显示或显示异常解决方法