QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187974 | #5506. Hyperloop | APJifengc | WA | 157ms | 9248kb | C++14 | 3.1kb | 2023-09-25 10:04:39 | 2023-09-25 10:04:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005, MAXD = 50005;
int T;
int n, m, k;
vector<pair<int, int>> e[MAXN];
typedef unsigned long long hash_t;
typedef __uint128_t ui;
const hash_t P = 998244353, B = 19260817;
hash_t BASE[MAXN];
struct SegmentTree {
struct Node {
int lc, rc;
hash_t hs;
} t[MAXN * 17];
int tot;
void clear() { tot = 0; }
int newNode() { ++tot; t[tot].lc = t[tot].rc = t[tot].hs = 0; return tot; }
void insert(int &p, int d, int l = 1, int r = k) {
if (!p) p = newNode();
t[p].hs = ((ui) t[p].hs + BASE[d - l]) % P;
if (l == r) return;
int mid = (l + r) >> 1;
if (d <= mid) insert(t[p].lc, d, l, mid);
else insert(t[p].rc, d, mid + 1, r);
}
bool cmp(int p, int q, int d, int e, int l = 1, int r = k) {
if (l == r) return t[p].hs + (d == l) > t[q].hs + (e == r);
int mid = (l + r) >> 1;
if (((ui) t[t[p].rc].hs + (mid < d && d <= r ? BASE[d - mid - 1] : 0)) % P ==
((ui) t[t[q].rc].hs + (mid < e && e <= r ? BASE[e - mid - 1] : 0)) % P)
return cmp(t[p].lc, t[q].lc, d, e, l, mid);
else
return cmp(t[p].rc, t[q].rc, d, e, mid + 1, r);
}
} st;
int dis[MAXN], root[MAXN], pre[MAXN];
bool vis[MAXN];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
int main() {
BASE[0] = 1;
for (int i = 1; i < MAXN; i++) BASE[i] = (ui) B * BASE[i - 1] % P;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) e[i].clear();
k = 0;
for (int i = 1; i <= m; i++) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
k = max(k, c);
e[u].push_back({ v, c });
e[v].push_back({ u, c });
}
st.clear();
for (int i = 1; i <= n; i++) root[i] = 0, dis[i] = INT_MAX / 2, pre[i] = 0, vis[i] = 0;
dis[1] = 0; q.push({ 0, 1 });
while (!q.empty()) {
int u = q.top().second; q.pop();
if (vis[u]) continue;
vis[u] = 1;
if (u != 1) {
root[u] = root[pre[u]];
st.insert(root[u], dis[u] - dis[pre[u]]);
}
for (auto [v, c] : e[u]) if (!vis[v]) {
if (dis[v] > dis[u] + c) {
dis[v] = dis[u] + c;
pre[v] = u;
q.push({ dis[v], v });
} else if (dis[v] == dis[u] + c) {
if (st.cmp(root[u], root[pre[v]], c, dis[v] - dis[pre[v]])) pre[v] = u;
}
}
}
vector<int> ans;
int u = n;
while (u) {
ans.push_back(u);
u = pre[u];
}
reverse(ans.begin(), ans.end());
printf("%lu\n", ans.size());
for (int i : ans) printf("%d ", i);
printf("\n");
}
return 0;
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9248kb
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
Wrong Answer
time: 157ms
memory: 8704kb
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 85 86 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...
result:
wrong answer Contestant's path is not optimal lexicographically (test case 2)