QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420084#8673. 最短路径wsyear10 5177ms356936kbC++173.9kb2024-05-24 14:31:482024-05-24 14:31:49

Judging History

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

  • [2024-05-24 14:31:49]
  • 评测
  • 测评结果:10
  • 用时:5177ms
  • 内存:356936kb
  • [2024-05-24 14:31:48]
  • 提交

answer

// Author: Klay Thompson
// Problem: C. 最短路径
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

vector<tuple<int, int, ll>> generate_edges(int n, int m, unsigned seed) {
  mt19937 gen(seed);
  vector<tuple<int, int, ll>> edges(m);
  unsigned max = -1u / n * n;
  auto sample = [&]() {
    unsigned x;
    do { x = gen(); } while(x >= max);
    return x % n + 1;
  };
  for(auto & [u, v, w] : edges) {
    u = sample();
    v = sample();
    w = gen();
  }
  return edges;
}

const int maxn = 3000010;
const ll inf = 1e18;

int n, m, t, vis[maxn], cur[maxn], rcur[maxn], from[maxn], rfrom[maxn];
ll dis[maxn], rdis[maxn];
unsigned seed;
vector<pll> e[maxn], re[maxn];
priority_queue<pll, vector<pll>, greater<pll>> q, rq;

ll solve(int s, int t) {
  vector<int> vec, rvec;
  q.emplace(0, s), dis[s] = 0, vec.emplace_back(s);
  rq.emplace(0, t), rdis[t] = 0, rvec.emplace_back(t);
  int x = -1;
  while (!q.empty() || !rq.empty()) {
    if (!q.empty()) {
      auto [cd, u] = q.top();
      vis[u] |= 1;
      if (vis[u] == 3) { x = u; break; }
      q.pop();
      if (dis[u] != cd) continue;
      while (from[u] && cur[from[u]] < SZ(e[from[u]])) {
        auto [v, w] = e[from[u]][cur[from[u]]++];
        if (dis[from[u]] + w < dis[v]) {
          dis[v] = dis[from[u]] + w, vec.emplace_back(v);
          from[v] = from[u], q.emplace(dis[v], v);
          break;
        }
      }
      while (cur[u] < SZ(e[u])) {
        auto [v, w] = e[u][cur[u]++];
        if (dis[u] + w < dis[v]) {
          dis[v] = dis[u] + w, vec.emplace_back(v);
          from[v] = u, q.emplace(dis[v], v);
          break;
        }
      }
    }
    if (!rq.empty()) {
      auto [cd, u] = rq.top();
      vis[u] |= 2;
      if (vis[u] == 3) { x = u; break; }
      rq.pop();
      if (rdis[u] != cd) continue;
      while (rfrom[u] && rcur[rfrom[u]] < SZ(re[rfrom[u]])) {
        auto [v, w] = re[rfrom[u]][rcur[rfrom[u]]++];
        if (rdis[rfrom[u]] + w < rdis[v]) {
          rdis[v] = rdis[rfrom[u]] + w, rvec.emplace_back(v);
          rfrom[v] = rfrom[u], rq.emplace(dis[v], v);
          break;
        }
      }
      while (rcur[u] < SZ(re[u])) {
        auto [v, w] = re[u][rcur[u]++];
        if (rdis[u] + w < rdis[v]) {
          rdis[v] = rdis[u] + w, rvec.emplace_back(v);
          rfrom[v] = u, rq.emplace(rdis[v], v);
          break;
        }
      }
    }
  }
  ll ans = inf;
  if (x == -1) { ans = -1; goto cle; }
  ans = dis[x] + rdis[x];
  for (int u : vec) for (auto [v, w] : e[u]) chkmn(ans, dis[u] + rdis[v] + w);
  cle:;
  for (int x : vec) {
    vis[x] = from[x] = rfrom[x] = cur[x] = rcur[x] = 0;
    dis[x] = rdis[x] = inf;
  }
  for (int x : rvec) {
    vis[x] = from[x] = rfrom[x] = cur[x] = rcur[x] = 0;
    dis[x] = rdis[x] = inf;
  }
  while (!q.empty()) q.pop();
  while (!rq.empty()) rq.pop();
  return ans;
}

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n >> m >> t >> seed;
  auto edges = generate_edges(n, m, seed);
  for (auto [u, v, w] : edges) {
    e[u].emplace_back(v, w);
    re[v].emplace_back(u, w);
  }
  rep (i, 1, n) sort(ALL(e[i]), [&](pll x, pll y) { return x.se < y.se; });
  rep (i, 1, n) sort(ALL(re[i]), [&](pll x, pll y) { return x.se < y.se; });
  rep (i, 1, n) dis[i] = rdis[i] = inf;
  while (t--) {
    int s, t;
    cin >> s >> t;
    cout << solve(s, t) << '\n';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 15ms
memory: 146252kb

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: 5
Accepted
time: 12ms
memory: 146016kb

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:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 2000 lines

Test #3:

score: 0
Wrong Answer
time: 66ms
memory: 146612kb

input:

1000 2000 2000 1526732796
400 914
837 927
7 271
873 60
934 156
981 329
973 512
276 54
540 278
605 230
681 555
636 706
955 618
640 214
859 696
267 595
38 839
309 12
484 919
746 49
948 337
607 638
438 163
817 869
95 518
534 376
369 331
665 64
736 970
154 41
510 425
876 907
143 803
270 403
350 286
131 ...

output:

14198403396
-1
23317797058
11552404306
16160464812
27144556597
-1
5570702410
-1
19513776618
10597134504
8945453029
20326028889
-1
12608727274
17050357023
-1
-1
15134668738
19589312812
32078322699
16255615559
-1
20150114514
15485138820
-1
5265380455
-1
19291857101
-1
-1
-1
28016363155
17619903738
-1
...

result:

wrong answer 3rd lines differ - expected: '20203456441', found: '23317797058'

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 1850ms
memory: 314776kb

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:

40161864
68019117
45803385
27805775
50282628
52244371
53707486
39687913
37675345
23367579
42401217
49451571
50703016
26615083
25983590
51209814
54647963
31222991
39092809
25257238
36267824
61379702
34199475
74771790
60747587
39809073
58284870
80407900
31005342
56604801
35033769
37667712
49769099
378...

result:

wrong answer 1st lines differ - expected: '36084543', found: '40161864'

Subtask #3:

score: 10
Accepted

Test #13:

score: 10
Accepted
time: 291ms
memory: 164796kb

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:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 10000 lines

Test #14:

score: 10
Accepted
time: 44ms
memory: 158156kb

input:

200000 100000 10000 1394653802
99794 128174
196511 141958
176353 6707
19037 95308
12331 132159
47825 12373
47277 130874
165656 114428
81800 12371
165878 128160
33280 71225
139344 138789
126396 182051
103407 151857
20873 18698
155652 38063
150807 191146
57310 174863
114490 88197
158133 29636
137962 1...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 10000 lines

Test #15:

score: 10
Accepted
time: 76ms
memory: 154424kb

input:

100000 100000 10000 913053279
28316 35031
36768 9164
74111 12192
71120 23394
97477 34141
50880 24433
99500 23365
99785 571
95784 50853
8313 70744
33410 27807
29073 96498
82964 79943
32999 84423
90798 98756
98245 89258
89589 49557
90152 40866
53406 41385
33889 39018
42199 52421
13784 26639
85311 5769...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 10000 lines

Test #16:

score: 10
Accepted
time: 25ms
memory: 145896kb

input:

1 1 10000 1920830832
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 10000 lines

Subtask #4:

score: 0
Time Limit Exceeded

Test #17:

score: 0
Time Limit Exceeded

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
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

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
Wrong Answer

Test #24:

score: 0
Wrong Answer
time: 3546ms
memory: 351020kb

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:

5484804025
1533240012
1192488694
1802115335
2152416868
1967964027
4286104406
2949358544
2695532146
4247923828
1250925907
2119783981
3440218754
1385409721
2759253009
2976144031
1632784703
3457597950
2259908728
3217046116
1927202358
5032568635
2286394605
1582695830
1940165963
2462020161
2165024874
372...

result:

wrong answer 1st lines differ - expected: '1547972368', found: '5484804025'

Subtask #7:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 5177ms
memory: 356936kb

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:

5597238229
4843999695
40316225381
7098730632
5525086105
7094821497
13915204006
4776897494
4367628219
5164037480
5129249062
4740514262
10252863490
9688011243
4596721509
5437894960
4847106102
7060409908
10404411558
5415910907
12554920884
2160161436
15638944600
5471414577
5079847290
6403967565
56859191...

result:

wrong answer 1st lines differ - expected: '3371897180', found: '5597238229'