QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344240 | #5506. Hyperloop | ucup-team1198# | AC ✓ | 1261ms | 55348kb | C++20 | 5.1kb | 2024-03-03 20:12:55 | 2024-03-03 20:12:56 |
Judging History
answer
#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
struct Node {
int val;
int left, right;
Node(int val): val(val), left(-1), right(-1) {}
Node() {}
};
const int MXNODES = 3'000'000;
const int USED = 1 << 22;
const int MAXN = 100100;
Node nodes[MXNODES];
int extras[17];
int nodes_cnt = 0;
const int MX = 50'050;
int dps[MAXN];
int par[MAXN];
void dfs(int t) {
if (t < 0)
return;
if (nodes[t].val & USED)
return;
nodes[t].val |= USED;
dfs(nodes[t].left);
dfs(nodes[t].right);
}
int n;
void collect_garbage() {
for (int i = 0; i < MXNODES; ++i)
nodes[i].val = (nodes[i].val & (~USED));
for (int i = 0; i < n; ++i) {
dfs(dps[i]);
}
for (int i = 0; i < 17; ++i)
dfs(extras[i]);
}
int node(int val) {
while (true) {
if (nodes_cnt == MXNODES) {
collect_garbage();
nodes_cnt = 0;
}
if (!(nodes[nodes_cnt].val & USED)) {
break;
}
++nodes_cnt;
}
nodes[nodes_cnt] = Node(val);
++nodes_cnt;
return nodes_cnt - 1;
}
int val(int T) {
return T == -1 ? 0 : (nodes[T].val ^ USED);
}
int get_left(int T) {
return T == -1 ? -1 : nodes[T].left;
}
int get_right(int T) {
return T == -1 ? -1 : nodes[T].right;
}
int add(int T, int left, int right, int x, int w = 0) {
int ans = node(val(T) + 1 + USED);
extras[w] = ans;
nodes[ans].left = get_left(T);
nodes[ans].right = get_right(T);
if (left + 1 == right)
return ans;
int mid = (left + right) / 2;
if (x < mid)
nodes[ans].left = add(get_left(T), left, mid, x, w + 1);
else
nodes[ans].right = add(get_right(T), mid, right, x, w + 1);
return ans;
}
void go_down(int T, int left, int right, vector<pair<int, int>>& to) {
if (T == -1)
return;
if (left + 1 == right) {
to.emplace_back(left, nodes[T].val ^ USED);
return;
}
int mid = (left + right) / 2;
go_down(nodes[T].right, mid, right, to);
go_down(nodes[T].left, left, mid, to);
}
bool cmp(const vector<pair<int, int>>& first, const vector<pair<int, int>>& second) {
for (int i = 0; i < first.size() && i < second.size(); ++i) {
if (first[i] != second[i]) {
return first[i] > second[i];
}
}
return first.size() > second.size();
}
const int INF = 1e5;
vector<pair<int, int>> G[MAXN];
vector<pair<int, int>> kek;
vector<pair<int, int>> lol;
void solve() {
fill(extras, extras + 17, -1);
int m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u; --v;
/*if (i < n)
u = i, v = (i + 1) % n;
else {
u = rand() % n;
v = rand() % n;
while (v == u)
v = rand() % n;
}
w = abs(u - v);*/
//cerr << u << ' ' << v << '\n';
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
vector<int> dist(n, INF);
dist[0] = 0;
set<pair<int, int>> Q;
vector<int> order;
Q.emplace(0, 0);
while (!Q.empty()) {
int v = Q.begin()->second;
order.emplace_back(v);
Q.erase(Q.begin());
for (auto [u, w] : G[v]) {
if (dist[u] > dist[v] + w) {
Q.erase(make_pair(dist[u], u));
dist[u] = dist[v] + w;
Q.emplace(dist[u], u);
}
}
}
fill(dps, dps + n, -2);
fill(par, par + n, -1);
dps[0] = -1;
for (int v : order) {
for (auto [u, w] : G[v]) {
if (dist[u] + w == dist[v]) {
// cur is lol
int var = add(dps[u], 0, MX, w);
kek.clear();
go_down(var, 0, MX, kek);
if (dps[v] == -2) {
swap(lol, kek);
par[v] = u;
dps[v] = var;
} else {
if (cmp(kek, lol)) {
swap(lol, kek);
par[v] = u;
dps[v] = var;
}
}
}
}
lol.clear();
}
int cur = n - 1;
vector<int> ans;
while (cur != 0) {
ans.emplace_back(cur);
cur = par[cur];
}
ans.emplace_back(0);
reverse(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (int x : ans)
cout << x + 1 << ' ';
cout << '\n';
for (int i = 0; i < n; ++i) {
G[i].clear();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tst;
cin >> tst;
while (tst--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5616kb
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: 233ms
memory: 40988kb
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:
ok correct (600 test cases)
Test #3:
score: 0
Accepted
time: 492ms
memory: 54500kb
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 24696 24717 24804 24757 24988 24231 25030 24601 94061 24443 24448 24563 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: 1261ms
memory: 55348kb
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 7169 7168 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: 611ms
memory: 53472kb
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 94659 94664 1697 31258 31267 31276 31231 99694 31227 31190 31223 91851 2489 2480 2483 98560 2507 2512 2515 2532 2529 99815 71980 74130 74129 99693 84670 84671 71536 30302 30301 30300 30299 30298 30297 30296 30295 30294 30293 30292 30291 30290 30289 30288 30287 30286 30285 26878 35089 708...
result:
ok correct (4 test cases)