网络最大流模板求调

P3376 【模板】网络最大流

Siegerkranz_2735 @ 2024-07-19 14:57:56

#include<bits/stdc++.h>
using namespace std;
struct Edge{
    int v,next;
    long long w;
}edge[10005];
int n,m,s,t,cnt,head[10005];
long long neck[205],ans;
int flag[205][205],vis[205],path[205];
void addedge(int u,int v,long long w){
    cnt++;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
bool bfs(){
    for(int i=1;i<=n;i++)vis[i]=0;
    queue<int> q;
    q.push(s),vis[s]=1,neck[s]=2147483648;
    while(!q.empty()){
        int now=q.front();
        q.pop();
        for(int i=head[now];i;i=edge[i].next){
            int v=edge[i].v;
            if(edge[i].w<=0)continue;
            if(vis[v])continue;
            neck[v]=min(neck[now],edge[i].w);
            path[v]=i;
            q.push(v);
            vis[v]=1;
            if(v==t)return 1;
        }
    }
    return 0;
}
void update(){
    int loc;
    for(int now=t;now!=s;now=edge[loc^1].v){
        int loc=path[now];
        edge[loc].w-=neck[t];
        edge[loc^1].w+=neck[t];
    }
    ans+=neck[t];
}
int main(){
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for(int i=1;i<=m;i++){
        int u,v;long long w;
        scanf("%d%d%lld",&u,&v,&w);
        if(!flag[u][v]){
            addedge(u,v,w);
            addedge(v,u,0);
            flag[u][v]=cnt-1;
            flag[v][u]=cnt;
        }
        else{
            edge[flag[u][v]].w+=w;
        }
    }
    for(;bfs();update());
    printf("%lld",ans);
    return 0;
}

by SleepWithMiku @ 2024-08-14 14:40:34

cnt初始为1


by SleepWithMiku @ 2024-08-14 14:41:09

还有你定义了两个loc


by SleepWithMiku @ 2024-08-14 14:43:14

@Siegerkranz_2735


|