运行不出结果QwQ(悬关)

P3376 【模板】网络最大流

WZRYWZWY @ 2024-02-23 08:11:39

rt,按着《算法竞赛进阶指南》打的,感觉一模一样QAQ

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = 1 << 31, N = 205, M = 10005;

int head[N], ver[M], edge[M], Next[M], d[N], /*当前弧优化*/now[M];
int n, m, s, t, tot, maxflow;
queue <int> q;

void add(int x, int y, int z) {
    ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
    ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
}

bool bfs() { // 在残量网络上构造分层图 
    memset(d, 0, sizeof(d));
    while (q.size()) q.pop();
    q.push(s); d[s] = 1; now[s] = head[s];
    while (q.size()) {
        int x = q.front(); q.pop();
        for (int i = head[x]; i; i = Next[i])
            if (edge[i] && !d[ver[i]]) {
                q.push(ver[i]);
                now[ver[i]] = head[ver[i]];
                d[ver[i]] = d[x] + 1;
                if (ver[i] == t) return 1;
            }
    }
    return 0; 
}

int dinic(int x, int flow) { // 在当前分层图上增广 
    if (x == t) return flow;
    int rest = flow, k, i;
    for (i = now[x]; i && rest; i = Next[i]) {
        now[x] = i; // 当前弧优化(避免重复遍历从 x 出发不可扩展的边) 
        if (edge[i] && d[ver[i]] == d[x] + 1) {
            k = dinic(ver[i], min(rest, edge[i]));
            if (!k) d[ver[i]] = 0; // 剪枝,去掉增广完毕的点
            edge[i] -= k;
            edge[i ^ 1] += k; 
            rest -= k;
        }
    }
    return flow - rest;
}

signed main() {
    cin >> n >> m >> s >> t;
    tot = 1;
    for (int i = 1; i <= m; i++) {
        int x, y, c; cin >> x >> y >> c;
        add(x, y, c);
    }
    int flow = 0;
    while (bfs())
        while (flow = dinic(s, inf)) maxflow += flow;
    cout << maxflow;
}

by ___Furina___ @ 2024-02-23 08:20:47

@WZRYWZWY 这一句话错了(笑):

const int inf = 1 << 31, N = 205, M = 10005;

应该改成:

const int inf = 2e9, N = 205, M = 10005;

by QWQ_123 @ 2024-02-23 08:26:01

@WZRYWZWY 1 << 31 太大了,改成 1 << 30 即可。


by WZRYWZWY @ 2024-02-23 08:31:19

@OIer_qyzy @QWQ_123 thanks.


by WZRYWZWY @ 2024-02-23 08:35:44

@OIer_qyzy @QWQ_123 嗯,但是不是 w<10^{31} 吗QAQ


by ___Furina___ @ 2024-02-23 08:38:26

@WZRYWZWY


by ___Furina___ @ 2024-02-23 08:40:35

@WZRYWZWY 德莉傻病毒已经入侵 OI 了吗?qnq


by WZRYWZWY @ 2024-02-23 08:45:17

@OIer_qyzy 不好意思打错数据范围了,我主要是想表达31次方((

话说为什么还是int的大小


by WZRYWZWY @ 2024-02-23 08:56:39

@QWQ_123 @OIer_qyzy

找到问题了,应该是 1ll << 31.

此贴结.


|