当前位置:网站首页>G: maximum flow problem
G: maximum flow problem
2022-06-28 14:00:00 【The wind blows the fallen leaves and the flowers flutter】
G : Maximum flow problem
Description
Give a directed graph of a network , Source point and sink point , Calculate its maximum flow . The number of vertices in the network is 1~N, The origin and destination are specified in the input .
Input
The first line gives four integers N(1 <= N <= 200), M(N <= M <= 5000),S,T, Indicates the number of nodes respectively 、 Number of directed edges 、 Source point serial number 、 Meeting point number .
Next M Each row contains three positive integers ui,vi,wi, It means the first one i The direction of a bar is from ui set out , arrive vi, The boundary right is wi.0<wi<=10000
Output
a line , Include a positive integer , Is the maximum flow of the network flow .
Sample Input
6 9 4 2
1 3 10
2 1 20
2 3 20
4 3 10
4 5 30
5 2 20
4 6 20
5 6 10
6 2 30Sample Output
50
Hint
Statement of question : Given a picture (n Nodes ,m side ), Each side has a capacity , Now you need to remove some items from the node s( It's called the source point ) Transport to node t( It's called a meeting point ), You can transfer from other nodes , Find the maximum transportation capacity .
Before introducing the solution to the maximum flow problem , First, I will introduce some concepts .
The Internet : A network is a directed weighted graph , Contains a source point and a sink point , No reverse parallel edges .
Network flow : Network flow is the flow on the network , Is defined in the network edge set E A nonnegative function on flow={flow(u,v)}, flow(u,v) Is the flow on the side .
Feasible flow : A network flow that satisfies the following two properties flow It's called feasible flow .
Capacity constraints : The actual flow on each side cannot exceed the changed maximum flow .
Conservation of flow : Except for the source s And meeting point t outside , The inflow of all internal nodes is equal to the outflow .
Source point s: The source point is mainly outflow , But it may also flow into .
Net output value of the source point = The sum of outflow - The sum of inflows .
Confluence t: The meeting point is mainly inflow , But it may also flow out .
The net input value of the sink = The sum of inflows - The sum of outflow .
For a network feasible flow flow, Net output equals net input , This is still the conservation of flow .
Network maximum flow : On the premise of satisfying capacity constraints and flow conservation , Find a network flow with the largest net output in the flow network .
Reverse arc : If from u To v The capacity of the side of is c , There is flow on this side f Flow through ( Called positive arc ), It's equivalent to v To u There is one with a capacity of 0 The edge of , Its flow is - f , This side is the reverse arc . The function of the reverse arc is mainly used to find the augmented path .
The meaning of reverse arc : The function of reverse arc is to make the currently selected arc give up automatically when there is a better decision . The reason why the reverse arc has flow is that if the forward arc has just been selected , Next time if there is a better strategy to make this f Flow into the sink , You can select the reverse arc , Will flow f revoke .
Residual network : Calculate the difference between capacity and flow on each side of the graph ( It is called residual capacity ), You can get the residual network . Note that due to the existence of reverse edges , The number of edges in the residual network may reach twice the number of edges in the original graph .
Augmentation path : Any one of the residual networks starts from s To t All the directed paths of the corresponding graph correspond to an augmented path in the original graph —— As long as the minimum value of all residues in the road is found d , Increase the flow on all corresponding edges d that will do , This process is called augmentation .
Maximum flow theorem : If the augmentation path cannot be found on the residual network , The current flow is the maximum flow ; conversely , If the current flow is not the maximum flow , Then there must be an expansion path .
Two 、AC Code
#include <iostream>
#include <cstring>
#include<queue>
#include<vector>
using namespace std;
int n,m;
const int maxn=1005;
int g[maxn][maxn];// Capacity
int f[maxn][maxn];// Traffic
int vis[maxn];
int pre[maxn];// Array of precursors
const int INF =1111111;
int bfs(int s,int t)
{
memset(pre,-1,sizeof(pre));// Initialize array
memset(vis,0,sizeof(vis));
queue<int > q;
vis[s]=1;// Recording time
q.push(s);// Join the queue
// cout<<s<<t<<endl;
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(!vis[i]&&g[now][i])
{
vis[i]=1;
pre[i]=now;
if(i==t)return 1;// When you get to the end, you quit
q.push(i);
}
}
}
return 0;
}
int u,v;
int EK(int s,int t)
{
int maxflow=0;
while(bfs(s,t))//bfs Find a path
{
int d=INF;
v=t;
while(v!=s)// Use the precursor to reset to the original s, And find the minimum flow , Meet capacity constraints
{
// Get precursor
u=pre[v];
// Find the minimum capacity on the path
if(d>g[u][v])d=g[u][v];
// Iterate forward
v=u;
}
maxflow+=d;// Maximum flow =+ Minimum capacity on all paths
// cout<<d<<endl;
v=t;
while(v!=s)
{
// Get precursor
u=pre[v];
// Find the augmentation path
g[u][v]-=d;
g[v][u]+=d;// Satisfy the flow constraint , The input and output streams are equal
if(f[v][u]>0)f[v][u]=-d;
else f[u][v]+=d;
// Iterate forward
v=u;
}
}
//cout<<maxflow<<endl;;
return maxflow;
}
int main()
{
int s,t;
while(cin>>n>>m>>s>>t)
{
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u][v]+=w;
}
cout<<EK(s,t)<<endl;
}
return 0;
}
边栏推荐
- Kubernetes 深入理解kubernetes(一)
- Notes on the use of official jeecg components (under update...)
- G : 最大流问题
- [机缘参悟-32]:鬼谷子-抵巇[xī]篇-面对危险与问题的五种态度
- 决策树预测红酒品质
- n-queens problem
- Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
- Pytorch model
- How to back up MySQL_ The most complete MySQL backup method in history
- Why will the new 5g standard bring lower TCO to the technology stack
猜你喜欢

How to set auto format after saving code in vscade

中国广电5G套餐来了,比三大运营商低,却没预期那么低

BERT为何无法彻底干掉BM25??

From PDB source code to frame frame object

《蛤蟆先生去看心里医生》阅读笔记

Websocket automatically disconnects in 1 minute

Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系

How to design data visualization platform

First knowledge of exception

yii2编写swoole的websocket服务
随机推荐
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
PC Museum - familiar and strange ignorant age
SPI interface introduction -piyu dhaker
How to design data visualization platform
ArrayList源码解析
[codec] write H264 decoder (1) from scratch
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
线程终止的 4 种方式
行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会
Pytorch Foundation
Double buffer drawing
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
Yii2 connects to websocket service to realize that the server actively pushes messages to the client
接雨水系列问题
药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
Kubernetes 深入理解kubernetes(一)
Embedded design and development project - liquid level detection and alarm system
(original) [Maui] realize "floating action button" step by step
【二叉树】从叶结点开始的最小字符串