32pts,Dinic求调

P3376 【模板】网络最大流

Somusomunia @ 2023-12-31 16:56:50

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e3+10;
const ll inf=1e15;
ll ans=0,w[maxn<<1],dep[maxn];
int n,m,s,t,u,v,val,top,cur=0;
int p[maxn<<1],h[maxn],nxt[maxn<<1],curh[maxn];
queue<int> q;
inline void add(int x,int y,int ww){
    nxt[++cur]=h[x],h[x]=cur,p[cur]=y,w[cur]=ww;
    nxt[++cur]=h[y],h[y]=cur,p[cur]=x,w[cur]=0;
}
inline ll dfs(int o,ll con){
    if(o==t) return con;
    ll res=0,minn;
    for(int i=curh[o];i&&con;i=nxt[i]){
        curh[o]=i;
        if(w[i]>0&&(dep[p[i]]==dep[o]+1)){
            minn=dfs(p[i],min(con,w[i]));
            if(!minn) dep[p[i]]=inf;
            w[i]-=minn;
            w[i^1]+=minn;
            res+=minn;
            con-=minn;
        }
    }
    return res;
}
inline bool bfs(){
    while(!q.empty()) q.pop();
    for(int i=1;i<=n;i++) dep[i]=inf;
    q.push(s),dep[s]=0,curh[s]=h[s];
    while(!q.empty()){
        top=q.front();
        q.pop();
        for(int i=h[top];i;i=nxt[i]){
            if(w[i]>0&&dep[p[i]]==inf){
                dep[p[i]]=dep[top]+1;
                curh[p[i]]=h[p[i]];
                q.push(p[i]);
                if(p[i]==t) return true;
            }
        }
    }
    return false;
}
int main(){
    cin>>n>>m>>s>>t;
    for(int i=1;i<=m;i++){
        cin>>u>>v>>val;
        add(u,v,val);
    }
    while(bfs()) ans+=dfs(s,inf);
    cout<<ans;
    return 0;
}

by keep_of_silence @ 2023-12-31 17:02:34

@Somusomunia cur初始化为1,这样你的^1之后改变的才是相反边


by keep_of_silence @ 2023-12-31 17:02:56

@Somusomunia 改了就对了


by Somusomunia @ 2024-01-02 16:00:43

@keep_of_silence 谢谢巨佬


by _Minecraft12345 @ 2024-03-09 15:38:34

啊啊啊警钟QAQ谢谢大佬


|