当前位置:网站首页>Maximum flow problem
Maximum flow problem
2022-06-24 21:58:00 【Weng Weiqiang】
"For me,my only dream is to help my family lead a better life"
---2021.8.27
1. Network flow :
In a directed graph
u,v Representative vertex G(u,v) For from u To v On the traffic

S Represents the source point ,T Representative meeting point
Maximum flow :
1. The maximum traffic on the source to sink path
2. How to find :
1. utilize dfs() Search the maximum flow from the source point to the sink point , But the maximum flow must be the minimum flow on the path . Then subtract the corresponding minimum flow from the positive side , The opposite side plus the corresponding minimum flow , This is how to build an augmentation path
2. And then use it bfs() To determine whether there is an augmented path from the source point to the sink point , No end , Find the maximum flow
Template title :
http://poj.org/problem?id=1273
AC Code :
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x7fffffff
using namespace std;
const int N = 205;
int g[N][N];
int n, m, deep[N], s, t;
int u, v, flow;
int maxflow;
queue<int> q;
bool bfs()
{
while (!q.empty())q.pop();
memset(deep, -1, sizeof(deep));
deep[s] = 0;
q.push(s);
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = 1; i <= m; ++i)
{
if (g[x][i] > 0 && deep[i]== -1)
{
deep[i] = deep[x] + 1;
q.push(i);
}
}
}
if (deep[t]>0) { return true; }
else { return false; }
}
// Based on the current hierarchical graph , Find all the Zengguang roads
int dfs(int x, int mx)// The current vertex sum and the maximum flow on this augmented path
{
int a;
if (x == t) { return mx; }
for (int i = 1; i <= m; ++i)
{
if (g[x][i] > 0 && deep[i] == deep[x] + 1 && (a = dfs(i, min(mx, g[x][i]))))// use dfs Go search Until we find the minimum traffic on this Zengguang road
{
// Update the residual network
g[x][i]-=a;
g[i][x] += a;// Reverse side building
return a;
}
}
return 0;
}
void solve()
{
while (bfs())// Until we can't find a Zengguang road to the destination
{
maxflow += dfs(s, INF);
}
}
int main()
{
while (scanf("%d %d", &n, &m)!=EOF)
{
maxflow = 0;
s = 1, t = m;
memset(g, 0, sizeof(g));
for (int i = 1; i <= n; ++i)
{
scanf("%d %d %d", &u, &v, &flow);
g[u][v] += flow;
}
solve();
printf("%d\n", maxflow);
}
return 0;
}边栏推荐
- (待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识
- leetcode-201_ 2021_ 10_ seventeen
- [精选] 多账号统一登录,你如何设计?
- 【吴恩达笔记】多变量线性回归
- C语言-关键字1
- 如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
- dp问题集
- Based on asp Net development of fixed assets management system source code enterprise fixed assets management system source code
- VSCode无网环境快速迁移开发环境(VIP典藏版)
- leetcode1863_ 2021-10-14
猜你喜欢

Datakit 代理实现局域网数据统一汇聚

leetcode-201_ 2021_ 10_ seventeen

字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?

即构「畅直播」上线!提供全链路升级的一站式直播服务

cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)

Vscode netless environment rapid migration development environment (VIP collection version)

【吴恩达笔记】机器学习基础

手动事务的几个类

应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案

Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
随机推荐
Redis+caffeine two-level cache enables smooth access speed
Prompt that the device has no permission when using ADB to connect to the device
leetcode:1504. 统计全 1 子矩形的个数
机器学习:线性回归
煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
Multiplexer select
将二维数组方阵顺时针旋转90°
拖动拖动拖动
02--- impossible phenomenon of longitudinal wave
介绍BootLoader、PM、kernel和系统开机的总体流程
WMI and PowerShell get TCP connection list
网络层 && IP
leetcode_1365
These map operations in guava have reduced my code by 50%
MySQL optimizes query speed
Blender FAQs
Direct attack on "three summers" production: good harvest news spreads frequently and summer broadcasting is in full swing
Guava中这些Map的骚操作,让我的代码量减少了50%
使用Adb连接设备时提示设备无权限
【Camera基础(一)】Camera摄像头工作原理及整机架构