首页 > 其他分享 >【LOJ101】最大流(Edmonds-Karp)

【LOJ101】最大流(Edmonds-Karp)

时间:2023-02-08 12:36:15浏览次数:43  
标签:Karp int flow infc head tot Edmonds vis LOJ101


problem

  • 给定n个点,m条边的有向图
  • 求源点s到汇点的最大流

solution

最大流模板,,不会看笔记吧。。。

codes

//Edmonds-Karp
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 110, maxm = 5010<<1;

int n, m, s, t;
int tot=1, head[maxn], Next[maxm], ver[maxm], flow[maxm];
void AddEdge(int x, int y, int z){
ver[++tot] = y; flow[tot] = z;
Next[tot] = head[x]; head[x] = tot;
ver[++tot] = x; flow[tot] = 0;
Next[tot] = head[y]; head[y] = tot;
}

LL infc[maxn], pre[maxn], maxflow;
bool vis[maxn];
bool bfs(){
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s); vis[s] = 1; infc[s] = 1<<30;
while(q.size()){
int x = q.front(); q.pop();
for(int i = head[x]; i; i = Next[i]){
int y = ver[i];
if(vis[y])continue;
if(!flow[i])continue;//1.当前边无流量返回
infc[y] = min(infc[x], (LL)flow[i]);//2.增广路上各边的最小剩余容量
pre[y] = i;//3.方案
vis[y] = 1; q.push(y);
if(y == t)return true;//4.到达汇点
}
}
return false;
}
void update(){
int x = t;
while(x != s){
int i = pre[x];
flow[i] -= infc[t];
flow[i^1] += infc[t];
x = ver[i^1];
}
maxflow += infc[t];
}

int main(){
cin>>n>>m>>s>>t;
for(int i = 1; i <= m; i++){
int x, y, z; cin>>x>>y>>z; AddEdge(x,y,z);
}
while(bfs())update();
cout<<maxflow<<'\n';
return 0;
}


标签:Karp,int,flow,infc,head,tot,Edmonds,vis,LOJ101
From: https://blog.51cto.com/gwj1314/6044056

相关文章

  • LOJ10150
    题目描述Hecy又接了个新任务:BE处理。BE中有一类被称为GBE。以下是GBE的定义:空表达式是GBE如果表达式A是GBE,则[A]与(A)都是GBE如果A与B都是GBE,那......
  • loj10135.「一本通 4.4 练习 2」祖孙询问
    SLOJP10135.「一本通4.4练习2」祖孙询问题目描述已知一棵n个节点的有根树。有m个询问,每个询问给出了一对节点的编号x和y,询问x与y的祖孙关系。输入格式输入第一行......
  • 一天star量破千,300行代码,特斯拉AI总监Karpathy写了个GPT的Pytorch训练库
    整理:公众号@机器之心本文仅做学术分享,如有侵权,请联系删除。如果说GPT模型是所向披靡的战舰,那么minGPT大概算是个头虽小但仍能乘风破浪的游艇了吧。最近,「史上最大AI模......