QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#560218#8673. 最短路径Fido_Puppy0 3097ms221872kbC++233.8kb2024-09-12 14:23:412024-09-12 14:23:42

Judging History

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

  • [2024-09-12 14:23:42]
  • 评测
  • 测评结果:0
  • 用时:3097ms
  • 内存:221872kb
  • [2024-09-12 14:23:41]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define rz resize
#define MP make_pair
#define MT make_tuple
#define IT iterator
#define fi first
#define se second
#define For(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define Rep(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define debug cerr << "ztxakking\n"
#define y0 ztxaknoi
#define y1 ztxakioi
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using vi = vector<int>;
template<typename T>
using V = vector<T>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 7;
const ll Inf = 0x3fffffffffffffff;
int n, m, q;
uint seed;
V<pil> G0[N], G1[N];
V<tuple<int, int, ll>> generate_edges(int n, int m, uint seed) {
  mt19937 gen(seed);
  V<tuple<int, int, ll>> edges(m);
  uint mx = -1u / n * n;
  auto sample = [&] () {
    uint x;
    do { x = gen(); } while (x >= mx);
    return x % n + 1;
  };
  for(auto &[u, v, w] : edges) {
    u = sample();
    v = sample();
    w = gen();
  } // edge (u, v, w)
  return edges;
}
struct Info {
  int c, u;
  ll w;
  Info() = default;
  Info(int c, int u, ll w) : c(c), u(u), w(w) {}
  friend bool operator < (Info x, Info y) {
    return x.w > y.w;
  }
};
priority_queue<Info> qL, qR;
ll dL[N], dR[N];
bitset<N> visL, visR;
vi xL, xR;
ll dist(int s, int t) {
  if (s == t) return 0;
  int p = -1;
  dL[s] = dR[t] = 0, xL.pb(s), xR.pb(t), visL[s] = visR[t] = 1;
  while (qL.size()) qL.pop();
  while (qR.size()) qR.pop();
  if (G0[s].size()) qL.emplace(0, s, G0[s][0].se);
  if (G1[s].size()) qR.emplace(0, t, G1[t][0].se);
  for (int t = 0; qL.size() && qR.size(); t ^= 1) {
    if (t) {
      auto [c, u, w] = qL.top(); qL.pop();
      int v = G0[u][c].fi;
      if (c + 1 < G0[u].size()) qL.emplace(c + 1, u, w - G0[u][c].se + G0[u][c + 1].se);
      if (!visL[v]) {
        dL[v] = w, visL[v] = 1, xL.pb(v);
        if (visR[v]) {
          p = v; break;
        }
        if (G0[v].size()) qL.emplace(0, v, w + G0[v][0].se);
      }
    } else {
      auto [c, u, w] = qR.top(); qR.pop();
      int v = G1[u][c].fi;
      if (c + 1 < G1[u].size()) qR.emplace(c + 1, u, w - G1[u][c].se + G1[u][c + 1].se);
      if (!visR[v]) {
        dR[v] = w, visR[v] = 1, xR.pb(v);
        if (visL[v]) {
          p = v; break;
        }
        if (G1[v].size()) qR.emplace(0, v, w + G1[v][0].se);
      }
    }
  }
  if (p == -1) return -1;
  ll ans = dL[p] + dR[p];
  for (int i : xL) {
    for (auto [j, w] : G0[i]) {
      if (w > (dL[p] - dL[i]) * 2) break;
      ans = min(ans, dL[i] + w + dR[j]);
    }
  }
  for (int i : xR) {
    for (auto [j, w] : G1[i]) {
      if (w > (dR[p] - dR[i]) * 2) break;
      ans = min(ans, dR[i] + w + dL[j]);
    }
  }
  for (int i : xL) visL[i] = 0, dL[i] = Inf;
  for (int i : xR) visR[i] = 0, dR[i] = Inf;
  xL.clear(), xR.clear();
  return ans;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m >> q >> seed;
  V<tuple<int, int, ll>> edges = generate_edges(n, m, seed);
  for (auto [u, v, w] : edges) G0[u].eb(v, w), G1[v].eb(u, w);
  For(i, 1, n) {
    auto cmp = [&] (pil x, pil y) { return x.se < y.se; };
    sort(all(G0[i]), cmp), sort(all(G1[i]), cmp);
  }
  For(i, 1, n) dL[i] = dR[i] = Inf;
  visL.reset(), visR.reset();
  while (q--) {
    int s, t; cin >> s >> t;
    cout << dist(s, t) << '\n';
  }
  cerr << (double)(clock()) / CLOCKS_PER_SEC << '\n';
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 5
Accepted
time: 1ms
memory: 6192kb

input:

4 8 5 1112792816
2 3
4 3
4 3
3 2
1 4

output:

3419197189
1798364963
1798364963
3986398077
2337967903

result:

ok 5 lines

Test #2:

score: 0
Runtime Error

input:

2000 2000 2000 3336994405
659 1650
1678 341
818 235
1380 1865
1927 1366
1233 1673
267 1698
775 1022
1255 1110
1533 1928
1854 169
1579 729
449 1335
943 583
360 50
795 926
1584 911
1924 604
280 309
1429 420
1107 1858
1466 76
265 1109
1077 622
245 1941
957 1434
1560 1128
122 51
229 925
826 1006
851 323...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #6:

score: 15
Accepted
time: 445ms
memory: 177628kb

input:

3000 3000000 10000 37461678
2873 1368
1757 2000
1262 1822
2484 1778
2055 2096
2545 366
2923 2028
1469 1874
691 631
1173 2967
894 2020
1207 881
373 236
1913 1923
1351 16
1066 2032
471 1561
1047 2043
457 145
2728 1752
2521 1199
1568 904
2515 543
1472 2161
748 2744
748 1908
912 172
2340 2494
977 267
10...

output:

36084543
49860181
45803385
27805775
41392651
43506617
39517515
39687913
37675345
23367579
37276839
32364058
50703016
26615083
25983590
51209814
42921191
31222991
39092809
25257238
36267824
60108033
34199475
45804995
35826034
34257048
38718065
55135658
31005342
41408425
35033769
37667712
42873640
378...

result:

ok 10000 lines

Test #7:

score: 15
Accepted
time: 327ms
memory: 136132kb

input:

3000 2000000 10000 2522167365
2102 2825
724 1689
2259 2561
1681 677
62 2183
2589 1214
926 1138
674 2610
1679 1607
1349 2461
2617 1599
457 2347
584 518
1506 554
2954 470
1027 893
1924 2
2624 2746
1366 2651
2236 2085
362 2871
1413 1763
2497 404
1507 1216
894 322
2221 2553
824 2374
1883 1507
2484 2504
...

output:

65574243
49955828
53828505
51865209
52351116
61557386
51116830
55590246
56377606
32235042
40593621
48849551
65887052
65047947
68965925
45241121
29819326
68037564
51238828
51815122
51454820
50482802
78004899
69718038
51304835
72570590
63002470
71137709
72879314
39737181
46218127
56704281
46947435
745...

result:

ok 10000 lines

Test #8:

score: 15
Accepted
time: 216ms
memory: 73312kb

input:

3000 1000000 10000 711905757
844 1281
882 1379
1448 2597
2686 1871
1556 677
337 871
825 248
1686 345
1259 775
422 763
2445 2585
1514 1028
90 1993
2203 2185
2965 2115
499 2266
2274 2635
713 450
2978 1453
1745 1010
11 350
1746 2622
1070 1458
438 1462
2936 2707
1797 2495
1929 873
1426 32
1696 548
2756 ...

output:

137380549
162704262
143745916
115032641
79062560
136541282
75207874
55127915
100171107
113209549
114113337
128511651
121886243
151535892
106186341
124611628
123504840
127411130
157283803
92948750
154286595
124377360
88897895
191915816
111939138
111074921
99047774
95249923
136436236
57049943
93591345...

result:

ok 10000 lines

Test #9:

score: 15
Accepted
time: 180ms
memory: 41628kb

input:

3000 500000 10000 4065069523
1355 22
595 1315
137 828
444 1241
483 1807
1852 377
1292 2452
478 1758
2712 2071
2243 1344
194 2765
2645 1718
2078 202
1860 2607
495 1091
2492 2800
2594 694
2021 2441
1393 1253
1378 2008
114 727
1019 196
1142 71
2787 2507
650 2675
2074 2132
2697 614
1611 1662
2687 358
13...

output:

283099212
197991417
240849607
272997490
378109456
160014053
252448699
281163198
280701476
178120202
189979017
272229633
267521047
219833816
183204444
275985942
208578258
148366474
287620336
264106800
220537155
167544642
306771926
200838815
179562301
313150724
246238367
194938277
197389047
201592436
...

result:

ok 10000 lines

Test #10:

score: 15
Accepted
time: 131ms
memory: 14568kb

input:

3000 100000 10000 2346395888
2334 174
757 2882
2571 2749
2571 1300
1511 2435
170 1648
107 465
2588 2135
1571 1754
2919 2295
717 129
1779 2941
1493 1505
1784 470
164 371
1381 1204
1644 1556
2234 1583
54 2836
815 777
1060 671
1147 1945
879 2968
2030 609
770 2226
2414 1944
1893 885
478 1705
643 439
135...

output:

1037539058
841259924
1119227208
936606501
1124792817
550785284
1187414290
1105329599
534927835
1079864539
1056661616
1426806296
1387193176
717428828
1083183267
793850415
455433261
527722167
1087705276
1140313309
1048197735
777649783
1066670304
1244984113
1535939812
1008629859
1033454877
1242231980
1...

result:

ok 10000 lines

Test #11:

score: 0
Runtime Error

input:

3000 3000 10000 397949456
418 2179
1809 996
1420 2230
204 2974
2416 2274
2601 2425
172 1604
263 2652
2446 2508
1807 1321
1619 2575
1918 735
201 2718
134 1960
2804 22
189 988
1949 39
2260 2933
22 1853
2721 761
911 2218
2189 1676
2461 2594
471 643
1645 1453
144 1601
2501 1592
53 1710
1452 596
352 2347...

output:


result:


Subtask #3:

score: 0
Runtime Error

Test #13:

score: 0
Runtime Error

input:

200000 200000 10000 1824322211
104482 112162
130667 13436
36792 142259
51832 97549
15358 180076
128251 92635
45296 195115
62109 38014
22014 86754
79735 103777
94797 96086
196760 5955
45622 59618
12995 62585
55686 156402
23085 68138
170749 148553
97603 160274
112975 22651
116322 190720
84774 57075
23...

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #17:

score: 0
Runtime Error

input:

200000 500000 10000 3113327438
68816 31422
174349 125983
18111 188786
84806 87249
142007 180723
95611 116398
104758 196349
77547 89859
120350 77199
110906 10209
177461 194861
115505 105566
27493 166237
15676 158290
86204 116010
159979 125659
132461 61989
194289 157721
18830 82910
166696 98162
125208...

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

200000 500000 10000 1843053063
3409 108359
168924 184622
13119 119837
109492 38050
97152 51201
49047 12472
183998 191613
193074 177289
194248 104409
15509 88499
61967 143398
4532 56790
196650 158711
63655 70744
140178 107299
63530 87330
127334 159237
7134 184418
125289 28604
176966 179527
181695 128...

output:


result:


Subtask #6:

score: 0
Runtime Error

Test #24:

score: 10
Accepted
time: 1900ms
memory: 214100kb

input:

100000 3000000 10000 3892765041
14843 34156
43390 49542
38564 95501
26194 87126
18638 53346
69414 47011
95472 58303
44370 77172
75652 90555
94386 31888
47911 9905
70599 97061
52764 24896
31445 15589
82314 43852
97155 93412
11834 45082
75614 42459
67802 32024
82389 4968
32860 62514
97630 28012
14839 ...

output:

1547972368
1533240012
1192488694
1802115335
1491444021
1888896300
1720188008
1762089620
1815841406
1831208977
1250925907
1756812381
2027344758
1385409721
1937527554
1877583272
1632784703
2090242303
1694524102
1818975564
1429598050
1599437722
2286394605
1416358110
1929044811
2022891575
1487757623
156...

result:

ok 10000 lines

Test #25:

score: 10
Accepted
time: 1847ms
memory: 145776kb

input:

100000 2000000 10000 2082503433
58880 78421
14548 46231
99049 88344
22391 26025
25236 34840
77162 82668
5667 67117
12870 11907
49640 62723
1755 5382
21226 76188
59145 70335
4679 71179
32038 73516
72621 41497
49627 18273
40479 91715
73191 40867
26710 98234
99898 23597
48509 24994
15771 1679
11605 571...

output:

2550292014
2088525319
2688949421
2128205012
3265691684
2456734516
1642691812
2983165881
3021975646
3122679543
2170817246
3179344726
2692378689
2567981687
2298179789
2073907330
2763664628
1855487724
2293201092
2937148401
3601836798
3010987679
3688384387
2780648332
2363790153
2058109458
2361104139
370...

result:

ok 10000 lines

Test #26:

score: 10
Accepted
time: 1837ms
memory: 77584kb

input:

100000 1000000 10000 272241824
59814 46877
53003 67113
92238 72676
61692 32219
21435 50927
52205 42516
15862 43227
81371 46643
23628 77996
17636 78876
35758 42470
56202 76312
91185 74357
8439 64147
30223 82246
36692 51645
77637 81452
11984 6570
85619 99036
17407 42226
88351 11665
66616 99537
49586 7...

output:

6418345560
3595930024
6543274734
5474244226
4520275434
5150335953
4277692210
5986379098
4573937177
7984631087
5980452817
4449908880
5275131238
6897728511
5018007685
6108102390
5945939138
5849450340
3278653602
6392948014
4711245030
5196851535
5369208668
4949967489
4687794608
6120501385
5234779104
587...

result:

ok 10000 lines

Test #27:

score: 0
Runtime Error

input:

100000 800000 10000 3920732141
4775 79843
49776 38439
38404 40798
12968 37738
29610 93752
55625 52220
16442 22778
27113 62439
99965 97616
3424 91930
51742 74410
23980 5990
57419 46555
82541 53857
52056 64189
30953 99783
76059 44236
32329 96980
63601 4870
1338 56203
12984 58609
1614 22674
85619 39445...

output:

6773118428
6016808197
5294603101
5431630355
6719722231
6061433622
8321347545
7106574205
4706280676
7514388136
7884070358
6290598631
5771030241
5766485204
5797476763
7907803690
4874071175
5172901782
5331218426
6303917257
6547572351
7979709668
5122570551
7117262205
6018533347
6498260413
6167971007
841...

result:


Subtask #7:

score: 0
Wrong Answer

Test #33:

score: 20
Accepted
time: 3082ms
memory: 221872kb

input:

200000 3000000 10000 3910662331
161257 40967
50546 86049
199665 199302
177403 36274
158790 143151
193304 78511
28032 149723
96394 37099
2157 76024
195400 34830
41933 147591
191613 96468
194837 67293
57992 63117
24749 6694
117818 87323
46130 53470
174812 24950
149173 124886
119910 54123
2297 124533
5...

output:

3371897180
3059012504
3899803743
4918664465
3834117056
3101758258
4211432244
3700678845
3320322266
4020388668
3314213999
4156507838
3045843635
3379778417
3201003504
4511026914
4847106102
3502897631
3579081638
3470448652
4322207527
2160161436
4372954162
3841655899
3367608876
3864513044
4225021719
377...

result:

ok 10000 lines

Test #34:

score: 0
Wrong Answer
time: 3097ms
memory: 149976kb

input:

200000 2000000 10000 2100400722
62191 109424
121705 6930
25558 16338
40896 18277
89580 91941
101052 38359
70931 193129
64894 4539
176145 124000
35472 141029
47953 56977
55967 69742
181344 13576
134393 21044
147760 104339
94483 20695
74776 75910
72389 23358
8081 158392
137419 172752
142139 187012
461...

output:

6283629982
4810712484
6173371551
5572267087
4533360250
6948138461
3763810206
6015991679
5138843815
5882103239
4651947070
5141390603
6054389311
7266250132
4406869907
5633307251
4787973112
5399509441
5723882182
4777557240
6651824180
3628172060
5633580623
7288229990
6224865765
6453345185
5384740659
464...

result:

wrong answer 3070th lines differ - expected: '5224449387', found: '-1'