当前位置:网站首页>最大流问题
最大流问题
2022-06-24 19:28:00 【翁炜强】
"For me,my only dream is to help my family lead a better life"
---2021.8.27
1.网络流:
在一个有向图中
u,v代表顶点 G(u,v)代表从u到v上的流量

S代表源点,T代表汇点
最大流:
1.源点到汇点路径上的最大流量
2.如何求:
1.利用dfs()去搜源点到汇点的最大流,但这个最大流必须是这条路径上的最小流量.然后正向边减去相应的最小流量,反向边加上相应的最小流量,这就是建立增广路径
2.然后利用bfs()去判断是否能存在源点到汇点的增广路,不存在结束,求出最大流
模板题目:
http://poj.org/problem?id=1273
AC代码:
#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; }
}
//基于当前分层图,找到所有的增广路
int dfs(int x, int mx)//当前顶点和以及这条增广路上的最大流
{
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]))))//用dfs去搜 直到找到这条增广路上的最小的流量
{
//更新残余网络
g[x][i]-=a;
g[i][x] += a;//反向建边
return a;
}
}
return 0;
}
void solve()
{
while (bfs())//直到找不到一条通往目的地的增广路即结束
{
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;
}边栏推荐
- Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
- RFC 793 why to send reset and when to send reset
- Structured interview of state-owned enterprises and central enterprises employment of state-owned enterprises Modou Interactive Employment Service steward
- 使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
- XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
- 字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
- Notes_ Vlan
- Slider控制Animator动画播放进度
- Functional analysis of ebpf sockops
- 多路转接select
猜你喜欢

Analysis of BBR congestion control state machine

Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached

【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构

memcached完全剖析–1. memcached的基础

XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor

memcached全面剖析–5. memcached的应用和兼容程序

手动事务的几个类

Implementing DNS requester with C language

What does CTO (technical director) usually do?
随机推荐
BBR bandwidth per second conversion logic
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
数据链路层 && 一些其他的协议or技术
Direct attack on "three summers" production: good harvest news spreads frequently and summer broadcasting is in full swing
【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
Antdb database online training has started! More flexible, professional and rich
C语言-关键字1
Debugging Analysis of Kernel panics and Kernel oopses using System Map
VSCode无网环境快速迁移开发环境(VIP典藏版)
TCP_ Nodelay and TCP_ CORK
力扣每日一题-第25天-496.下一个更大元素Ⅰ
【吴恩达笔记】卷积神经网络
Analysis of tcpdump packet capturing kernel code
Apple mobile phone can see some fun ways to install IPA package
VirtualBox virtual machine installation win10 Enterprise Edition
Datakit 代理实现局域网数据统一汇聚
Introduce the overall process of bootloader, PM, kernel and system startup
CondaValueError: The target prefix is the base prefix. Aborting.
Failed to open after installing Charles without any prompt
Multiplexer select