题目链接:

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 ;
}

最新文章

  1. 【WP开发】不同客户端之间传输加密数据
  2. CheckBox复选框全选以及获取值
  3. html/css小练习2
  4. [CareerCup] 18.7 Longest Word 最长的单词
  5. c# AES加解密并转ASCII码
  6. 14.python笔记之paramiko
  7. July 23rd, Week 30th Saturday, 2016
  8. Inside TSQL Querying - Chapter 3. Query Tuning
  9. [已决解]关于Hadoop start-all.sh启动问题
  10. CANOE入门(二)
  11. Qemu-KVM管理
  12. linux centos 访问根目录 not accessable
  13. 【分布式架构】“spring cloud”与“dubbo”微服务的对比
  14. VS2010错误
  15. 精读JavaScript模式(二)
  16. 浅谈移动应用的跨平台开发工具(Xamarin和React Native)
  17. 字典树---2001 POJ Shortest Prefixes(找最短前缀)
  18. Linux命令之which
  19. IDEA ULTIMATE 2019.1 注册码,亲测可用
  20. Unlink of file &#39;xx&#39; failed. Should I try again? (y/n) 解决办法

热门文章

  1. python下一个转码的问题
  2. Python实现返回指定范围内的所有素数
  3. 洛谷P1275 魔板
  4. 简单几步实现 IOS UITextField输入长度的控制
  5. linker 错误解决办法 地图
  6. 主键约束 primary key
  7. PDO中构建事务处理的应用程序
  8. ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps (线段树:无序数组找到第一个小于val)
  9. P2308 添加括号(区间DP)
  10. django--权限(1)初识