ISAP

2171. EK求最大流

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
const int M=1e6+5;
int h[N],ne[M],e[M],w[M],tot=1;
void add(int from,int to,int wi) {
e[++tot]=to;
w[tot]=wi;
ne[tot]=h[from];
h[from]=tot;
}
int n,m,S,T;
int dep[N],gap[N];
void bfs() {
dep[T]=1;
queue<int>q;
q.push(T);
while(!q.empty()) {
int now=q.front();
gap[dep[now]]++;//统计深度的个数
q.pop();
for(int i=h[now];i;i=ne[i]) {
int to=e[i];
if(dep[to])continue;
q.push(to);
dep[to]=dep[now]+1;
}
}
}
int cur[N];
int dfs(int now,int sum) {
if(now==T)return sum;
int ans=0;
for(int i=cur[now];i&&sum;i=ne[i]) {
cur[now]=i;
int to=e[i];
if(w[i]>0&&dep[now]==dep[to]+1) {
int k=dfs(to,min(sum,w[i]));
ans+=k;
sum-=k;
w[i]-=k;
w[i^1]+=k;
}
}
if(sum==0)return ans;
//流不完
if(--gap[dep[now]]==0)dep[S]=n+1;
gap[++dep[now]]++;
return ans;
}
int ISAP() {
int ans=0;
bfs();
while(dep[S]<=n) {
memcpy(cur,h,sizeof(h));
ans+=dfs(S,1e9);
}
return ans;
}
int main() {
cin>>n>>m>>S>>T;
while(m--) {
int x,y,wi;
cin>>x>>y>>wi;
add(x,y,wi);
add(y,x,0);
}
cout<<ISAP()<<endl;
return 0;
}
posted @   basicecho  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示