QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#293666#5506. HyperloopahsoltanAC ✓13410ms33748kbC++202.2kb2023-12-29 15:37:042023-12-29 15:37:05

Judging History

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

  • [2023-12-29 15:37:05]
  • 评测
  • 测评结果:AC
  • 用时:13410ms
  • 内存:33748kb
  • [2023-12-29 15:37:04]
  • 提交

answer

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

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 2137
#endif

const int N = 100100;
const ll INF64 = 2e18;
int n, m;
vector<pair<int, int>> adj[N];
vector<pair<int, int>> dag[N];
int dp[N];
bool vis[N];

vector<ll> go(int s) {
  vector<ll> dst(n, INF64);
  priority_queue<pair<ll, int>> q;
  dst[s] = 0;
  q.push({0, s});
  while (!q.empty()) {
    ll d = -q.top().first;
    int u = q.top().second;
    q.pop();
    if (d != dst[u]) {
      continue;
    }
    for (auto [v, w] : adj[u]) {
      if (d + w < dst[v]) {
        dst[v] = d + w;
        q.push({-dst[v], v});
      }
    }
  }
  return dst;
}

void dfs(int u, int x) {
  dp[u] = 0;
  for (auto [v, w] : dag[u]) {
    if (dp[v] == -1) {
      dfs(v, x);
    }
    dp[u] = max(dp[u], dp[v] + (w == x));
  }
}

void cut(int u, int x) {
  vis[u] = true;
  for (int i = 0; i < ssize(dag[u]); i++) {
    auto [v, w] = dag[u][i];
    if (dp[v] + (w == x) == dp[u]) {
      if (!vis[v]) {
        cut(v, x);
      }
    } else {
      swap(dag[u][i], dag[u].back());
      dag[u].pop_back();
      i--;
    }
  }
}

void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    adj[i].clear();
    dag[i].clear();
  }
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--; v--;
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
  }
  auto ds = go(0);
  auto dt = go(n - 1);
  vector<int> ww;
  for (int i = 0; i < n; i++) {
    for (auto [j, w] : adj[i]) {
      if (ds[i] + w + dt[j] == ds[n - 1]) {
        dag[i].push_back({j, w});
        ww.push_back(w);
      }
    }
  }
  sort(ww.begin(), ww.end(), greater<int>());
  ww.resize(unique(ww.begin(), ww.end()) - ww.begin());
  for (int w : ww) {
    for (int i = 0; i < n; i++) {
      dp[i] = -1;
      vis[i] = false;
    }
    dfs(0, w);
    cut(0, w);
  }
  vector<int> ans;
  ans.push_back(0);
  while (ans.back() != n - 1) {
    ans.push_back(dag[ans.back()][0].first);
  }
  cout << ssize(ans) << '\n';
  for (int u : ans) {
    cout << u + 1 << " \n"[u == ans.back()];
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8260kb

input:

2
4 6
1 2 1
1 3 2
2 3 1
2 4 2
3 4 1
1 4 4
6 11
1 2 9
2 3 12
3 4 3
4 5 5
5 6 10
6 1 22
2 4 9
3 6 1
4 6 5
2 5 2
3 5 8

output:

3
1 2 4
5
1 2 5 3 6

result:

ok correct (2 test cases)

Test #2:

score: 0
Accepted
time: 370ms
memory: 8844kb

input:

600
320 1547
204 81 13768
232 97 9939
97 249 3719
201 109 14322
183 132 40881
142 143 1
275 186 24548
18 236 7907
30 317 11845
131 130 1
311 300 11704
141 92 41925
174 191 32128
119 120 1
184 183 1
310 309 1
283 270 25477
233 141 36076
212 92 13770
307 110 40656
218 137 14033
180 85 41892
200 199 44...

output:

184
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...

result:

ok correct (600 test cases)

Test #3:

score: 0
Accepted
time: 1021ms
memory: 33748kb

input:

4
100000 220000
48940 43355 42347
77914 77913 1
45236 82683 42904
22563 16038 34866
81537 81538 43088
49803 51485 25497
63071 25523 14336
44102 39850 43782
13607 92386 16724
98711 73651 46840
17775 16801 28765
5757 98829 13508
85095 48444 1
9198 43003 32678
14461 14462 1
20245 48742 18138
89120 8911...

output:

35000
1 24721 24648 24647 24700 24305 24272 99392 25020 25051 25070 24443 24448 25109 25096 43056 43055 43054 43053 43052 43051 43050 43049 43048 43047 43046 43045 43044 43043 43042 43041 43040 43039 43038 43037 43036 43035 43034 43033 43032 43031 43030 43029 43028 43027 43026 43025 43024 43023 4302...

result:

ok correct (4 test cases)

Test #4:

score: 0
Accepted
time: 2731ms
memory: 25092kb

input:

4
100000 160000
5533 94547 28459
14992 20984 20548
70133 92512 27510
9013 9012 304
13621 40571 47787
305 306 262
6987 6988 135
16234 16992 40656
26246 49196 27701
19103 60272 44055
91532 91531 38290
70778 68341 35147
32524 32523 13
85786 50300 40970
49277 29735 13942
43446 34519 42455
77623 17031 34...

output:

316
1 2 3 4 5 6 97410 97409 26434 26435 26436 26437 98883 1370 1369 1368 92157 92158 4815 4816 4817 4818 50181 16985 89607 89608 24674 16979 16980 38428 13232 13233 13234 13664 13663 95009 7166 7165 7172 7163 24798 24799 11787 31031 53551 7309 7310 35482 7933 25067 32714 32715 44194 2068 72216 79593...

result:

ok correct (4 test cases)

Test #5:

score: 0
Accepted
time: 13410ms
memory: 29148kb

input:

4
100000 160000
89517 25671 43802
21059 21058 1
35299 91834 43615
53383 53382 1
27213 39161 17202
10715 4050 30342
44100 44099 1
24162 28648 7378
19022 23084 37734
66056 97934 14651
31566 89391 23215
91038 91037 1
47695 47696 6099
99142 99143 1
83908 73654 15060
15551 22001 8896
55190 55189 1
26536 ...

output:

632
1 94652 94653 31254 31269 31264 31173 31238 31281 31164 31289 31200 31205 91851 99690 91847 2469 99496 2507 2566 2537 2514 2547 2552 71980 74130 74129 99693 84670 88173 88172 88171 88170 88169 88168 88167 88166 88165 88164 88163 88162 88161 88160 88159 88158 88157 88156 91942 91941 91940 91939 9...

result:

ok correct (4 test cases)