QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#293663#5506. HyperloopahsoltanRE 0ms8428kbC++202.1kb2023-12-29 15:32:542023-12-29 15:32:55

Judging History

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

  • [2023-12-29 15:32:55]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:8428kb
  • [2023-12-29 15:32:54]
  • 提交

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];

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) {
  for (int i = 0; i < ssize(dag[u]); i++) {
    auto [v, w] = dag[u][i];
    if (dp[v] + (w == x) == dp[u]) {
      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());
  assert(ssize(ww) <= 250);
  for (int w : ww) {
    for (int i = 0; i < n; i++) {
      dp[i] = -1;
    }
    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: 8428kb

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: -100
Runtime Error

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:


result: