Dinic 28pts求条

P3376 【模板】网络最大流

hexz01 @ 2024-06-23 20:44:13

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=207, M=5007;
int n, m, s, t;
int head[N], to[M<<1], nxt[M<<1], tot=1, now[N];
ll val[M<<1], dis[N];
ll ans=0;
queue<int> q;
void add(int u, int v, ll w){
    tot++;
    nxt[tot]=head[u];
    head[u]=tot;
    to[tot]=v;
    val[tot]=w;
}
int bfs(){
    memset(dis, 0x3f, sizeof(dis));
    while(q.size())
        q.pop();
    q.push(s);
    dis[s]=0;
    now[s]=head[s];
    while(q.size()){
        int u=q.front();q.pop();
        for(int i=head[u];i;i=nxt[i]){
            int v=to[i];
            if(val[i]>0 && dis[v]==0x3f3f3f3f3f3f3f3f){
                q.push(v);
                now[v]=head[v];
                dis[v]=dis[u]+1;
                if(v==t)
                    return 1;
            }
        }
    }
    return 0;
}
int dfs(int x, ll flow){
    if(x==t || !flow)
        return flow;
    ll ans=0;
    for(int i=now[x];i && flow;i=nxt[i]){
        now[x]=i;
        int v=to[i];
        if(val[i]>0 && dis[v]==dis[x]+1){
            ll f=dfs(v, min(flow, val[i]));
            if(f==0)
                dis[v]=0x3f3f3f3f3f3f3f3f;
            val[i]-=f;
            val[i^1]+=f;
            ans+=f;
            flow-=f;
        }
    }
    return ans;
}
int main(){
    cin>>n>>m>>s>>t;
    for(int i=1;i<=m;i++){
        int u, v;
        ll w;
        cin>>u>>v>>w;
        add(u, v, w);add(v, u, w);
    }
    while(bfs()) ans+=dfs(s, 0x3f3f3f3f3f3f3f3fLL);
    cout<<ans<<endl;
    return 0;
}

AC#9 #11 #12 其他WA


by __ryp__ @ 2024-06-23 21:00:42

反边容量是 0 啊 @hexz01


by hexz01 @ 2024-06-24 06:54:20

谢谢 @ryp


by hexz01 @ 2024-06-24 06:58:00

@ryp 不过现在76分了


by __ryp__ @ 2024-06-24 13:18:51

@hexz01 dfs 返回的是不是应该是 ll,不懂这题卡不卡


by hexz01 @ 2024-06-25 07:53:12

是的,改过了谢谢。


|