QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642182#1217. 归程enze1145140 0ms0kbC++205.1kb2024-10-15 11:34:082024-10-15 11:34:09

Judging History

你现在查看的是最新测评结果

  • [2024-10-15 11:34:09]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-10-15 11:34:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define pb push_back

const ld pi = 3.14159265358979323846;

int mod = 998244353;
ll INF = (ll)1e18;

template<typename T>
T chmax(T a, T b) {
    return a > b ? a : b;
}

template<typename T>
T chmin(T a, T b) {
    return a > b ? b : a;
}

const int N = (int)4e6 + 1, M = N * 2, S = 21;

struct Kruskal {
    vector<int> h, w, e, ne, node, dep;
    vector<vector<int>> fa, kr;
    int n, idx, t;

    const int depth = 21;

    Kruskal(int _n) {
        n = _n;
        idx = 0;

        dep.resize(n * 4 + 1, (int)1e9);
        fa.resize(n * 4 + 1);

        ne.resize(n * 4 + 1, 0);
        h.resize(n * 4 + 1, -1);
        e.resize(n * 4 + 1, 0);
        w.resize(n * 4 + 1, 0);

        node.resize(n * 4 + 1, 0);

        for (int i = 0; i <= n * 4; i++) {
            fa[i] = vector<int>(depth + 1);
            node[i] = i;
        }
    }

    int find(int x) {
        return x == node[x] ? x : node[x] = find(node[x]);
    }

    void build(int u, int v) {
        e[idx] = v, ne[idx] = h[u], h[u] = idx++;
        e[idx] = u, ne[idx] = h[v], h[v] = idx++;
    }

    void kruskal() {
        for (int k = n, i = 1; i <= kr.size(); i++) {
            int du = find(kr[i - 1][0]), dv = find(kr[i - 1][1]);

            if (du != dv) {
                node[du] = node[dv] = ++k;
                w[k] = kr[i - 1][2];
                build(k, du);
                build(k, dv);
                t = k;

                if (k == 2 * n - 1) {
                    break;
                }
            }
        }
    }

    void bfs(int rt) {
        dep[rt] = 1;
        dep[0] = 0;

        queue<int> q;
        q.push(rt);

        while (!q.empty()) {
            int u = q.front();
            q.pop();

            for (int i = h[u]; i != -1; i = ne[i]) {
                int j = e[i];

                if (dep[j] > dep[u] + 1) {
                    dep[j] = dep[u] + 1;

                    q.push(j);
                    fa[j][0] = u;

                    for (int k = 1; k < depth; k++) {
                        fa[j][k] = fa[fa[j][k - 1]][k - 1];
                    }
                }
            }
        }
    }

    int lca(int u, int v) {
        if (find(u) != find(v))
            return -1;

        if (dep[u] < dep[v])
            swap(u, v);

        for (int i = depth - 1; i >= 0; i--) {
            if (dep[fa[u][i]] >= dep[v]) {
                u = fa[u][i];
            }
        }

        if (u == v)
            return u;

        for (int i = depth - 1; i >= 0; i--) {
            if (fa[u][i] != fa[v][i]) {
                u = fa[u][i];
                v = fa[v][i];
            }
        }

        return fa[u][0];
    }
};

ll h[N], f[N], v[N], e[M], ne[M], w[M], dp[M], n, m, idx;

void add(int u, int v, int c) {
    e[idx] = v, w[idx] = c, ne[idx] = h[u], h[u] = idx++;
}

bool cmp(vector<int> a, vector<int> b) {
    return a[2] > b[2];
}

void dfs(int u, int fa, const Kruskal &kr) {
    for (int i = kr.h[u]; i != -1; i = kr.ne[i]) {
        int j = kr.e[i];

        if (j == fa)
            continue;

        dfs(j, u, kr);
        dp[u] = chmin(dp[u], dp[j]);
    }

    dp[u] = chmin(dp[u], f[u]);
}

void solve() {
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        h[i] = -1;
    }

    idx = 0;

    Kruskal kr(n);

    for (int i = 0; i < m; i++) {
        int u, v, l, a;
        cin >> u >> v >> l >> a;

        add(u, v, l);
        add(v, u, l);

        kr.kr.pb({u, v, a});
    }

    ll qr, k, s, d = 0;
    cin >> qr >> k >> s;

    if (!m)
        return;

    sort(kr.kr.begin(), kr.kr.end(), cmp);

    for (int i = 0; i <= n * 10 + 1; i++) {
        f[i] = dp[i] = INF;
        v[i] = 0;
    }

    f[1] = dp[1] = f[0] = dp[0] = 0;

    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<>> q;
    q.push({0, 1});

    while (!q.empty()) {
        auto p = q.top();
        q.pop();

        ll u = p.second;

        if (v[u])
            continue;

        v[u] = 1;

        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];

            if (f[j] > f[u] + w[i]) {
                f[j] = f[u] + w[i];
                q.push({f[j], j});
            }
        }
    }

    kr.kruskal();
    kr.bfs(kr.t);

    dfs(kr.t, -1, kr);

    for (int i = 0; i < qr; i++) {
        ll v0, p0;
        cin >> v0 >> p0;

        ll v = (v0 + k * d % n - 1 + n) % n + 1;
        ll p = (p0 + k * d % (s + 1)) % (s + 1);

        for (int i = 20; i >= 0; i--) {
            if (!kr.fa[v][i])
                continue;

            if (kr.w[kr.fa[v][i]] > p) {
                v = kr.fa[v][i];
            }
        }

        cout << (d = dp[v]) << endl;
    }
}

signed main() {
    freopen("return.in", "r", stdin);
    freopen("return.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(0);

    int t = 1;
    cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 0
Dangerous Syscalls

input:

3
1 0
0 0 1
1 0
0 0 1
1 0
0 0 1

output:


result:


Test #2:

score: 0
Dangerous Syscalls

input:

3
6 9
1 6 395 1
6 3 13 1
6 4 798 1
6 5 403 1
4 5 2 1
3 1 97 1
5 2 8 1
3 5 187 1
3 2 640 1
10 0 1
4 1
3 1
5 1
3 1
6 0
5 1
3 1
5 1
5 0
3 0
6 7
4 3 4 1
2 5 26 1
6 4 30 1
2 4 1000 1
3 1 445 1
5 6 92 1
1 2 29 1
10 0 1
4 1
5 0
1 0
5 0
3 1
4 1
2 1
4 1
3 1
4 1
6 7
4 2 87 1
2 5 23 1
1 2 16 1
4 3 24 1
6 4 4 1...

output:


result:


Test #3:

score: 0
Dangerous Syscalls

input:

3
50 121
37 15 454 1
23 5 386 1
28 38 894 1
17 31 8 1
38 43 387 1
5 43 308 1
18 39 15 1
34 9 506 1
34 49 15 1
50 3 119 1
13 27 483 1
45 17 835 1
25 2 966 1
24 30 71 1
37 19 28 1
43 7 905 1
10 4 107 1
37 35 416 1
20 13 18 1
47 20 16 1
6 24 116 1
12 47 974 1
48 41 809 1
10 47 17 1
9 42 13 1
16 36 892 ...

output:


result:


Test #4:

score: 0
Dangerous Syscalls

input:

3
100 259
62 44 8 1
26 69 30 1
37 34 280 1
36 8 80 1
12 47 2 1
78 98 17 1
35 16 914 1
91 75 7 1
80 76 943 1
93 45 3 1
51 100 518 1
26 59 146 1
76 73 231 1
23 73 11 1
64 10 858 1
51 54 7 1
51 86 696 1
59 40 150 1
17 20 64 1
60 43 16 1
78 7 926 1
46 2 440 1
26 82 922 1
32 12 46 1
31 72 756 1
64 70 876...

output:


result:


Test #5:

score: 0
Dangerous Syscalls

input:

3
1403 4000
659 1257 556 1
428 883 77 1
398 429 623 1
934 998 916 1
408 1318 15 1
595 299 249 1
1243 483 668 1
335 1229 560 1
1357 697 607 1
91 555 4 1
131 508 847 1
591 254 219 1
1004 1319 11 1
209 78 299 1
328 532 21 1
1001 1215 12 1
768 981 762 1
1316 254 126 1
584 1338 583 1
125 869 29 1
745 330...

output:


result:


Test #6:

score: 0
Dangerous Syscalls

input:

3
134023 400000
106936 27428 617 1
31553 30342 971 1
19790 1355 127 1
49584 42179 26 1
28910 65772 18 1
48591 55180 17 1
4998 88813 195 1
82176 121958 759 1
21077 25885 341 1
120910 35270 883 1
27537 33116 20 1
78351 18357 6 1
101608 66216 426 1
30213 43293 998 1
37468 49783 586 1
103877 17557 19 1
...

output:


result:


Test #7:

score: 0
Dangerous Syscalls

input:

3
1500 1499
1263 1264 197 832549205
1407 1408 695 932716365
1146 1147 40 750669441
1386 1387 484 927439941
1178 1179 83 778216556
241 242 317 128333415
1189 1190 759 779845130
1447 1448 288 967694365
1041 1042 783 693510784
913 914 798 601420727
1359 1360 840 901837603
1225 1226 191 800290452
998 99...

output:


result:


Test #8:

score: 0
Dangerous Syscalls

input:

3
1500 1499
214 215 746 103777342
1305 1306 809 863481069
780 781 193 489096281
1380 1381 333 918631020
1299 1300 53 866441833
981 982 823 625528911
606 607 551 354810974
345 346 910 191333196
1475 1476 254 977255759
227 228 736 118721990
1378 1379 137 916242486
55 56 168 28186639
1338 1339 981 8937...

output:


result:


Test #9:

score: 0
Dangerous Syscalls

input:

3
1500 1499
186 187 314 94742289
75 76 261 39423111
1034 1035 125 648876320
1428 1429 832 941472266
1181 1182 504 752620972
342 343 486 178026302
440 441 192 240994251
1431 1432 552 945810062
1229 1230 159 790119569
1194 1195 565 761837664
113 114 256 55257526
636 637 238 352763203
1213 1214 756 781...

output:


result:


Test #10:

score: 0
Dangerous Syscalls

input:

3
200000 199999
168039 12763 765 279320236
183939 16430 198 6881839
122193 47956 163 21807713
67770 169599 261 994936289
74818 123071 837 825259015
150448 32275 987 90179474
155426 115119 567 807410643
143919 114116 968 915726797
92951 9762 998 671084406
150297 117878 862 107998837
91701 134269 653 ...

output:


result:


Test #11:

score: 0
Dangerous Syscalls

input:

3
200000 199999
176394 15779 785 127180026
118840 52233 628 69674288
1971 33381 302 757977279
72869 125444 355 183017412
104923 147605 520 839789866
124478 39387 865 582607027
155179 30904 563 287043678
62654 32772 741 221773407
175009 23990 438 117663422
177376 176873 136 848504696
57250 16505 54 8...

output:


result:


Test #12:

score: 0
Dangerous Syscalls

input:

3
134023 400000
67041 63272 992 180592857
41473 75825 108 301222188
114213 118633 12 580124132
20125 31290 30 519396153
90723 84443 189 316168642
84070 23184 683 111413871
112204 69103 389 388055662
115520 39583 310 273512441
105629 49771 973 81918254
112276 117987 26 749496507
121962 108299 838 256...

output:


result:


Test #13:

score: 0
Dangerous Syscalls

input:

3
134023 400000
58760 3502 768 215875099
107385 86405 15 527479997
132742 112734 946 444077360
17162 37315 564 392834909
112439 74725 25 747453202
132349 88516 327 84395270
73108 131771 99 18974938
109915 50777 727 263001501
62336 37521 630 149446391
41770 2858 621 21949981
130072 108091 323 4229302...

output:


result:


Test #14:

score: 0
Dangerous Syscalls

input:

3
134023 400000
15491 25039 962 444781241
43770 90154 968 416678205
19896 66733 917 208667421
15023 2102 692 444508565
74033 113231 3 504471242
94078 25801 8 624427171
102961 120670 491 241484370
78528 3616 27 586135212
107319 5083 876 40723199
28998 77345 384 304581393
116304 47118 591 153577459
81...

output:


result:


Test #15:

score: 0
Dangerous Syscalls

input:

3
1403 4000
63 230 9 792459870
250 10 521 435710193
186 75 483 41567023
185 1006 566 219013432
1148 740 142 41210356
486 1050 48 488430239
1218 60 5 939467812
369 1002 373 377523109
877 1206 29 656561482
980 659 81 342719745
544 17 958 234525425
880 1010 85 63567532
579 238 394 313448548
631 102 8 6...

output:


result:


Test #16:

score: 0
Dangerous Syscalls

input:

3
1403 4000
65 36 108 94567299
497 73 930 399127210
814 128 811 105804086
300 479 8 925063872
644 583 749 61035098
769 1221 9 756940473
129 1005 650 421450316
434 116 793 38919578
420 253 20 693812030
313 170 12 810090183
1296 689 837 148940335
32 491 5 909075814
720 1358 30 797814306
661 572 954 35...

output:


result:


Test #17:

score: 0
Dangerous Syscalls

input:

3
134023 400000
124828 37415 961 94076145
12469 128238 20 641194816
130567 15737 167 118907136
131297 6336 25 524919668
116982 130433 24 756819767
33316 74251 964 98831248
67280 6664 493 47288237
119344 53718 25 275115234
123245 128903 701 193512252
67851 82761 537 434699228
105776 133640 27 7409207...

output:


result:


Test #18:

score: 0
Dangerous Syscalls

input:

3
134023 400000
93897 30157 297 268792428
132559 55246 25 703055243
78340 95366 168 238768162
14465 13469 535 64125745
18273 112312 807 440189389
47009 122943 109 38388604
132558 29279 424 51116970
7442 50269 90 474483522
77415 104844 996 533591796
65098 57707 23 806817064
20920 120036 439 419244143...

output:


result:


Test #19:

score: 0
Dangerous Syscalls

input:

3
134023 400000
8769 5728 809 19387679
57063 91233 797 253655114
130290 72440 967 32496257
91843 24946 657 501122161
49847 43516 108 85279737
9697 18896 695 273697557
46300 88649 364 14955048
20408 46673 788 79075025
105581 10860 9 806715014
35442 17634 946 64078166
74381 121008 17 680814138
33256 6...

output:


result:


Test #20:

score: 0
Dangerous Syscalls

input:

3
134023 400000
102887 57011 25 621065092
104930 92183 31 572560306
118658 64299 848 501142203
131931 87158 65 200398712
132887 33804 15 654384648
46738 88508 15 616094154
113013 63932 174 188235708
51888 86629 17 551871892
12748 98037 218 210399397
85016 84392 818 377250424
126145 20672 857 1336467...

output:


result: