萌新刚学 OI,调不动了,求助 37pts 的 Dinic

P3376 【模板】网络最大流

SIXIANG32 @ 2021-06-01 21:48:58

又 WA 又 TLE 了,Dinic 磨人,救救孩子!/kk

#include <iostream>
#include <queue>
#include <cstring>
#define int long long
#define MAXN 100000
#define QWQ cout << "QWQ" << endl;
using namespace std;
int min(int x, int y) {return ((x < y) ? (x) : (y));}
struct node {
    int id, val;
    int next;
} gra[MAXN + 10];
int n, m, s, t;
int tot = 0, head[MAXN + 10], dis[MAXN + 10], Now[MAXN + 10];
void link(int x, int y, int z) {
    gra[++tot].id = y, gra[tot].val = z;
    gra[tot].next = head[x], head[x] = tot;
    gra[++tot].id = x, gra[tot].val = 0;
    gra[tot].next = head[y], head[y] = tot;
}
bool bfs() { 
    memset(dis, 0, sizeof(dis));
    queue <int> que;
    que.push(s), dis[s] = 1, Now[s] = head[s];
    while(!que.empty()) {
        int fr = que.front(); que.pop();
        for(int p = head[fr]; p; p = gra[p].next) {
            int v = gra[p].id, w = gra[p].val;
            if(w && !dis[v]) {
                que.push(v);
                Now[v] = head[v];
                dis[v] = dis[fr] + 1;
                if(v == t) return 1;
            }
        }
    }
    return 0;
}
int Dinic(int u, int over) {
    if(u == t) return over;
    int p, res = over, k;
    for(p = Now[u]; p && res; p = gra[p].next) {
        int v = gra[p].id, w = gra[p].val;
        Now[u] = v; 
        if(w && dis[v] == dis[u] + 1) {
            k = Dinic(v, min(res, w));
            if(!k) dis[v] = 0x7f7f7f7f;
            gra[p].val -= k;
            gra[p ^ 1].val += k;
            res -= k;
        }
    }
    return over - res;
}
signed main() {
    cin >> n >> m >> s >> t;
    for(int p = 1, x, y, z; p <= m; p++) {
        cin >> x >> y >> z;
        link(x, y, z);
    }
    int maxn = 0, rest = 0;
    while(bfs())
        maxn += (rest = Dinic(s, 0x7f7f7f7f)), cout << rest << endl;
    cout << maxn << endl;
}

求助/kel


by SIXIANG32 @ 2021-06-01 22:24:46

@happyChristmas 谢谢 qwq


by Rainbow_qwq @ 2021-06-01 22:25:26

初始化 边数 =1


by Spasmodic @ 2021-06-01 22:27:23

哦草,我傻逼


by SIXIANG32 @ 2021-06-01 22:28:02

@Rainbow_sjy❤OI 好家伙我日常脑抽,谢谢巨佬!qwq!


by _Clown_ @ 2021-06-01 22:33:05

真•刚学 \texttt{OI}


上一页 |