啊啊啊要疯了76pts求助!!!!!

P3376 【模板】网络最大流

AMlhd @ 2024-11-10 23:54:17

8 #9 #11wa了

#include <bits/stdc++.h>

#define N 500010
#define MAXX 1000000000001

using namespace std;
int n, m, s, t;
int u,v;
long long w;
long long ans, dis[N];
int tot = 1;
int now[N], head[N];

struct node {
    int to, nxt;
    long long val;
}e[N];

inline void add(int u, int v, long long w) {
    e[++tot].to = v;
    e[tot].val += w;
    e[tot].nxt = head[u];
    head[u] = tot;

    e[++tot].to = u;
    e[tot].val += 0;
    e[tot].nxt = head[v];
    head[v] = tot;
}

inline int bfs() {
    for(int i = 1; i <= n; ++i) {
        dis[i] = MAXX;
    }
    queue<int> q;
    q.push(s);
    dis[s] = 0;
    now[s] = head[s];
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        for(int i = head[x]; i; i = e[i].nxt) {
            int v = e[i].to;
            if(e[i].val > 0 && dis[v] == MAXX) {
                q.push(v);
                now[v] = head[v];
                dis[v] = dis[x] + 1;
                if(v == t) 
                    return 1;
            }
        }
    }
    return 0;
}

inline int dfs(int x, long long sum) { 
    if(x == t) 
        return sum;
    long long k, res = 0;
    for(int i = now[x]; i && sum; i = e[i].nxt) {
        now[x] = i; 
        int v = e[i].to;
        if(e[i].val > 0 && (dis[v] == dis[x] + 1)) {
            k = dfs(v, min(sum, e[i].val));
            if(k == 0) 
                dis[v] = MAXX; 
            e[i].val -= k;
            e[i ^ 1].val += k;
            res += k; 
            sum -= k; 
        }
    }
    return res;
}

int main() {
    cin >> n >> m >> s >> t;
    for(int i = 1; i <= m; ++i) {
        cin >> u >> v >> w;
        add(u, v, w);
    }
    while(bfs())
        ans += dfs(s, MAXX);
    cout << ans << endl;
    return 0;
}

by Lil_Tx @ 2024-11-21 13:59:52

可能存在重边,输入改一下就可以


|