当前位置:网站首页>G : 最大流问题
G : 最大流问题
2022-06-28 13:27:00 【风吹落叶花飘荡】
G : 最大流问题
Description
给出一张网络有向图,源点及汇点,计算其最大流。 该网络中的顶点编号为1~N,发点和汇点在输入中指定。
Input
第一行给出四个整数N(1 <= N <= 200), M(N <= M <= 5000),S,T,分别表示节点数量、有向边数量、源点序号、汇点序号。
接下来M行每行包括三个正整数ui,vi,wi,表示第 i条有向边从 ui出发,到达 vi,边权为 wi。0<wi<=10000
Output
一行,包括一个正整数,为该网络流最大流。
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
问题表述:给定一幅图(n个结点,m条边),每一条边有一个容量,现在需要将一些物品从结点s(称为源点)运送到结点t(称为汇点),可以从其他结点中转,求最大的运送量。
在介绍最大流问题的解决方法之前,先介绍几个概念.
网络:网络是一个有向带权图,包含一个源点和一个汇点,没有反向平行边。
网络流:网络流即网上的流,是定义在网络边集E上的一个非负函数flow={flow(u,v)}, flow(u,v)是边上的流量。
可行流:满足以下两个性质的网络流flow称为可行流。
容量约束:每条边的实际流量不能超过改变的最大流量。
流量守恒:除了源点s和汇点t之外,所有内部节点流入量等于流出量。
源点s:源点主要是流出,但也有可能流入。
源点的净输出值=流出量之和-流入量之和。
汇点t:汇点主要是流入,但也有可能流出。
汇点的净输入值=流入量之和-流出量之和。
对于一个网络可行流flow,净输出等于净输入,这仍然是流量守恒。
网络最大流:在满足容量约束和流量守恒的前提下,在流网络中找到一个净输出最大的网络流。
反向弧:若从u到v的边的容量为c ,这条边上有流量 f 流过(称为正向弧),则相当于v到u有一条容量为0的边,其流量为- f ,这条边就是反向弧。反向弧的作用主要是用于寻找增广路。
反向弧的意义:反向弧的作用是起到有更优决策的时候会使当前选择的弧会自动放弃。反向弧有流量的原因是因为如果刚刚选择了正向弧,下一次如果存在更优策略使这f的流量流入汇点,就可以选择反向弧,将流量 f 撤销。
残余网络:计算出图中的每条边上容量与流量之差(称为残余容量),即可得到残余网络。注意由于反向边的存在,残余网络中的边数可能到达原图中边数的两倍。
增广路径:残余网络中任何一条从s到t的有向道路都对应一条原图中的增广路径 —— 只要求出该道路中所有残量的最小值d ,把对应的所有边上的流量增加d 即可,这个过程称为增广。
最大流定理:如果残留网络上找不到增广路径,则当前流为最大流;反之,如果当前流不为最大流,则一定有增广路径。
二、AC代码
#include <iostream>
#include <cstring>
#include<queue>
#include<vector>
using namespace std;
int n,m;
const int maxn=1005;
int g[maxn][maxn];//容量
int f[maxn][maxn];//流量
int vis[maxn];
int pre[maxn];//前驱数组
const int INF =1111111;
int bfs(int s,int t)
{
memset(pre,-1,sizeof(pre));//初始化数组
memset(vis,0,sizeof(vis));
queue<int > q;
vis[s]=1;//记录时间
q.push(s);//加入队列
// 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;//到终点了就退出
q.push(i);
}
}
}
return 0;
}
int u,v;
int EK(int s,int t)
{
int maxflow=0;
while(bfs(s,t))//bfs找路径
{
int d=INF;
v=t;
while(v!=s)//利用前驱回置到最初s,并求出最小流,满足容量约束
{
//获取前驱
u=pre[v];
//求路径上的最小容量
if(d>g[u][v])d=g[u][v];
//向前迭代
v=u;
}
maxflow+=d;//最大流=+所有路径上的最小容量
// cout<<d<<endl;
v=t;
while(v!=s)
{
//获取前驱
u=pre[v];
//求增广路径
g[u][v]-=d;
g[v][u]+=d;//满足流量约束,输入输出流相等
if(f[v][u]>0)f[v][u]=-d;
else f[u][v]+=d;
//向前迭代
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;
}
边栏推荐
- PHP obtains the beginning and end time of the month according to the month and year
- PCB understand Wang, are you? I am not
- Professional English calendar questions
- Why do more and more users give up swagger and choose apifox
- iNFTnews | 科技巨头加快进军Web3和元宇宙
- Embedded design and development project - liquid level detection and alarm system
- Centos7——安装mysql5.7
- 5A同步整流芯片 20V转12V2A/5V4.5A大电流 24W大功率同步整流芯片 大电流降压IC FS2462
- Mature case and source code of hash quiz game system development technology
- 为什么越来越多的用户放弃 Swagger,选择Apifox
猜你喜欢

Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性

Pytorch Foundation

中国数据库技术大会(DTCC)特邀科蓝SUNDB数据库专家精彩分享

Recognize the startup function and find the user entry

Mobile web training day-1
![Buuctf:[wustctf2020] plain](/img/0f/a7973d3f7593f2464e48609e27d7bd.png)
Buuctf:[wustctf2020] plain

How to solve the problem that the computer wireless network does not display the network list

Make an ink screen electronic clock, cool!

行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会

2.01 backpack problem
随机推荐
How to open an account of Huatai Securities? How to handle the account opening is the safest
抢做意大利岛主?刘强东两月套现66亿 疑一次性5.6亿“紧急转账”急购欧洲海上皇宫
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
Which company has a low rate for opening a securities account? How to open an account is the safest
MySQL multi table joint query
Mysql database literacy, do you really know what a database is
iNFTnews | 科技巨头加快进军Web3和元宇宙
yii2连接websocket服务实现服务端主动推送消息给客户端
我呕血收集融合了来自各路经典shell书籍的脚本教学,作为小白的你快点来吧
Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability
Introduction to PWN (1) binary Basics
电驴怎么显示服务器列表,(转)如何更新电驴服务器列表(eMule Server List)
G1垃圾收集器中重要的配置参数及其默认值
Tiantian mathematics serial 53: February 22
移动Web实训DAY-2
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account
1015. picking flowers
PHP crawls web pages for specific information
Class structure in C language - dot
895. longest ascending subsequence