QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#671059#143. 最大流(随机数据)Qingyyx100 ✓7ms6256kbC++205.0kb2024-10-24 10:31:322024-10-24 10:31:32

Judging History

This is the latest submission verdict.

  • [2024-10-24 10:31:32]
  • Judged
  • Verdict: 100
  • Time: 7ms
  • Memory: 6256kb
  • [2024-10-24 10:31:32]
  • Submitted

answer

#include <bits/stdc++.h>
#define ll long long
#define int ll
#define enl putchar('\n')
#define all(x) (x).begin(),(x).end()
#define debug(x) printf(" "#x":%d\n",x);
using namespace std;
const int N = 100 + 5, M = 5000;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
typedef pair<int, int> pii;
char buf[1 << 21], * p1 = buf, * p2 = buf, obuf[1 << 21], * o = obuf, of[35];
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline ll qpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a % mod; n >>= 1; a = a * a % mod; }return res; }
template <class T = int>inline T read() { T s = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc())if (c == '-')f = -1; for (; isdigit(c); c = gc())s = s * 10 + c - '0'; return s * f; }
inline void read(int* a, int n) { for (int i = 1; i <= n; ++i)a[i] = read(); }
inline int inal(char* s) { int n = 0; for (s[0] = gc(); !isalpha(s[0]); s[0] = gc()); for (; isalpha(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline int indi(char* s) { int n = 0; for (s[0] = gc(); !isdigit(s[0]); s[0] = gc()); for (; isdigit(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline void outd(auto* a, int n) { for (int i = 1; i <= n; ++i)printf("%d ", a[i]); enl; }
int n, m, S, T, totnode;
int head[N], tot, cur[N], gap[N], ht[N], ex[N];  // 高度; 超额流; gap 优化 gap[i] 为高度为 i 的节点的数量
struct node {
    int to, nxt;
    ll val;
}e[M << 1];
inline void add(int u, int v, ll w) {
    e[tot] = node {v, head[u], w};
    head[u] = tot++;

    e[tot] = node {u, head[v], 0};
    head[v] = tot++;
}

stack<int> B[N];  // 桶 B[i] 中记录所有 ht[v]==i 的v
int level = 0;    // 溢出节点的最高高度
int push(int u) {      // 尽可能通过能够推送的边推送超额流
    bool init = u == S;  // 是否在初始化
    for (int i = head[u]; ~i; i = e[i].nxt) {
        const int& v = e[i].to, & w = e[i].val;
        if (!w || init == false && ht[u] != ht[v] + 1 ||
            ht[v] == inf)  // 初始化时不考虑高度差为1
            continue;
        int k = init ? w : min(w, ex[u]);
        // 取到剩余容量和超额流的最小值,初始化时可以使源的溢出量为负数。
        if (v != S && v != T && !ex[v]) B[ht[v]].push(v), level = max(level, ht[v]);
        ex[u] -= k, ex[v] += k, e[i].val -= k, e[i ^ 1].val += k;  // push
        if (!ex[u]) return 0;  // 如果已经推送完就返回
    }
    return 1;
}

void relabel(int u) {  // 重贴标签(高度)
    ht[u] = inf;
    for (int i = head[u]; ~i; i = e[i].nxt)
        if (e[i].val) ht[u] = min(ht[u], ht[e[i].to]);
    if (++ht[u] < totnode) {  // 只处理高度小于 n 的节点
        B[ht[u]].push(u);
        level = max(level, ht[u]);
        ++gap[ht[u]];  // 新的高度,更新 gap
    }
}

bool bfs() {
    memset(ht, 0x3f, sizeof(ht));
    queue<int> q;
    q.push(T), ht[T] = 0;
    while (q.size()) {  // 反向 BFS, 遇到没有访问过的结点就入队
        int u = q.front();
        q.pop();
        for (int i = head[u]; ~i; i = e[i].nxt) {
            const int& v = e[i].to;
            if (e[i ^ 1].val && ht[v] > ht[u] + 1) ht[v] = ht[u] + 1, q.push(v);
        }
    }
    return ht[S] != inf;  // 如果图不连通,返回 0
}

// 选出当前高度最大的节点之一, 如果已经没有溢出节点返回 0
int select() {
    while (B[level].size() == 0 && level > -1) level--;
    return level == -1 ? 0 : B[level].top();
}
int HLPP() {                  // 返回最大流
    if (!bfs()) return 0;  // 图不连通
    memset(gap, 0, sizeof(gap));
    for (int i = 1; i <= totnode; i++)
        if (ht[i] != inf) gap[ht[i]]++;  // 初始化 gap
    ht[S] = totnode;
    push(S);  // 初始化预流
    int u;
    while ((u = select())) {
        B[level].pop();
        if (push(u)) {  // 仍然溢出
            if (!--gap[ht[u]])
                for (int i = 1; i <= totnode; i++)
                    if (i != S && i != T && ht[i] > ht[u] && ht[i] < totnode + 1)
                        ht[i] = totnode + 1;  // 这里重贴成 n+1 的节点都不是溢出节点
            relabel(u);
        }
    }
    return ex[T];
}
void solve() {
    n = read(), m = read(), S = read(), T = read();
    totnode = n;
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= m; ++i) {
        int u = read(), v = read(), w = read();
        add(u, v, w);
    }
    printf("%lld\n", HLPP());
}
signed main(signed argc, char const* argv[]) {
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    //=============================================================
    int TxT = 1;
    // TxT = read();
    while (TxT--)
        solve();
    //=============================================================
#ifdef LOCAL
    end :
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 12.5
Accepted
time: 1ms
memory: 5976kb

input:

52 275 1 2
11 18 1
18 48 9
10 15 1
11 19 1
10 20 1
3 14 1
8 16 1
31 32 2147483647
10 42 9
5 14 1
3 15 1
5 17 1
6 50 9
1 6 9
28 29 2147483647
18 40 9
43 42 2147483647
1 9 9
9 20 1
1 7 9
24 6 9
39 38 2147483647
4 14 1
38 37 2147483647
5 46 9
3 18 1
15 44 9
4 17 1
32 33 2147483647
28 9 9
32 9 9
26 12 9...

output:

729

result:

ok single line: '729'

Test #2:

score: 12.5
Accepted
time: 1ms
memory: 6064kb

input:

67 4489 14 1
25 63 19983
49 18 26963
9 29 23009
25 30 10286
45 6 14693
61 11 8464
12 19 29821
39 36 2365
12 7 20737
56 51 21002
9 63 14701
15 10 24386
21 36 25930
49 21 10680
56 11 25508
26 27 2101
46 4 1770
16 56 19722
23 8 28411
67 32 28897
45 62 22880
30 38 13226
37 56 18650
10 57 700
62 53 19659...

output:

1025243

result:

ok single line: '1025243'

Test #3:

score: 12.5
Accepted
time: 1ms
memory: 5844kb

input:

100 1029 1 2
39 96 19
68 19 19
16 33 1
17 25 1
74 22 19
50 23 19
46 29 19
70 24 19
27 92 19
50 25 19
6 36 1
34 80 19
72 19 19
48 13 19
11 86 19
19 86 19
100 99 2147483647
4 39 1
60 9 19
76 7 19
34 100 19
98 97 2147483647
15 25 1
14 94 19
5 40 1
4 38 1
46 34 19
90 89 2147483647
42 39 19
58 27 19
3 39...

output:

4693

result:

ok single line: '4693'

Test #4:

score: 12.5
Accepted
time: 1ms
memory: 6008kb

input:

100 500 64 68
97 1 597234350
42 59 1020828575
52 59 1341185789
46 82 534859215
84 98 1408384018
95 85 97421544
50 51 1658946459
71 91 1071433566
16 5 577259372
79 16 941940144
32 66 2144021311
42 94 132280559
100 83 2093384600
34 98 1633024304
31 69 735801701
68 13 632197336
70 25 868338831
60 91 14...

output:

4259958644

result:

ok single line: '4259958644'

Test #5:

score: 12.5
Accepted
time: 1ms
memory: 6256kb

input:

100 1500 30 87
12 52 1212854316
66 34 500229329
28 30 1905848380
45 10 1906211267
35 5 1227091997
14 10 797678626
42 39 2119948760
80 55 263028757
72 32 1402091192
2 70 114204531
53 87 1885940117
39 68 1262963681
20 100 363298998
81 19 475298425
86 17 276841422
95 43 940479356
85 55 1720319570
40 65...

output:

17139501202

result:

ok single line: '17139501202'

Test #6:

score: 12.5
Accepted
time: 1ms
memory: 6132kb

input:

100 5000 12 73
5 90 596775756
35 20 226786760
28 31 1775982092
79 17 743002845
10 19 150120683
83 96 901953035
91 62 809520329
2 61 1024423315
30 91 1374494188
93 26 751944004
82 82 727762428
1 43 502389284
84 87 1379778919
52 32 1459460146
71 15 983677176
18 3 249963037
80 32 828290820
40 99 159181...

output:

37381805875

result:

ok single line: '37381805875'

Test #7:

score: 12.5
Accepted
time: 7ms
memory: 6192kb

input:

100 5000 13 28
74 16 599476
99 76 112185
76 68 887056
13 2 181381
23 72 214611
10 15 955272
57 53 163306
81 44 721618
68 62 71172
70 44 233121
13 52 701794
77 40 298244
54 28 626039
26 63 829000
25 14 91588
97 62 980457
17 15 572847
100 75 273645
4 65 344467
17 47 299474
40 19 270752
50 68 804106
21...

output:

2193636882

result:

ok single line: '2193636882'

Test #8:

score: 12.5
Accepted
time: 3ms
memory: 6196kb

input:

100 5000 66 90
35 39 966842
3 56 577708
38 60 515530
3 73 351251
29 27 508007
56 70 185615
73 51 331650
6 32 589603
29 96 822851
9 99 335209
20 45 806531
60 10 460779
93 21 203582
77 27 391590
3 14 315530
86 41 234991
53 69 96865
97 15 203159
14 43 815111
4 24 337097
88 79 288209
64 34 806690
13 26 ...

output:

4340954172

result:

ok single line: '4340954172'