QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377819 | #5506. Hyperloop | ckiseki# | AC ✓ | 569ms | 61016kb | C++20 | 4.7kb | 2024-04-05 18:22:16 | 2024-04-05 18:22:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
#include <experimental/iterator>
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
namespace std {
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p) {
return o << '(' << p.first << ',' << p.second << ')';
}
}
constexpr int64_t kInf64 = 1LL << 60;
// 0-base
pair<vector<array<int, 3>>, vector<int>> shortest_dag(const auto &e, int s, int n) {
vector<vector<pair<int,int>>> g(n);
for (auto [x, y, w] : e) {
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
vector<int64_t> d(n, kInf64);
vector<int> ord;
priority_queue<pair<int64_t, int>> pq;
pq.emplace(d[s] = 0, s);
while (not pq.empty()) {
auto [l, u] = pq.top();
pq.pop();
if (l == -d[u]) {
for (auto [v, w] : g[u])
if (d[u] + w < d[v])
pq.emplace(-(d[v] = d[u] + w), v);
ord.push_back(u);
}
}
vector<array<int, 3>> ret;
for (int i = 0; i < n; ++i) {
for (auto [j, w] : g[i]) {
if (d[i] + w == d[j])
ret.push_back({i, j, w});
}
}
return pair{ret, ord};
}
const int maxn = 100025;
struct List {
using u16 = uint16_t;
int sz;
pair<u16, u16> e[320];
List() : sz(0), e{} {}
void add(u16 val, u16 rep = 1) {
auto it = lower_bound(e, e + sz, pair<u16, u16>(val, -1U), greater<>());
if (it < e + sz && it->first == val) {
it->second += rep;
return;
}
int j = it - e;
for (int i = sz - 1; i >= j; i--)
e[i + 1] = e[i];
e[j] = {val, rep};
++sz;
}
};
bool cmp(const List &a, const List &b) {
for (int i = 0; i < a.sz && i < b.sz; i++) {
if (a.e[i] != b.e[i]) {
return a.e[i] > b.e[i];
}
}
assert(a.sz == b.sz);
return false;
}
List l[maxn / 4], cur, tmp;
int mapping[maxn];
int dep[maxn];
int pa[maxn], paw[maxn];
void solve() {
int n, m;
cin >> n >> m;
vector<array<int, 3>> es(m);
for (int i = 0; i < m; i++) {
auto &[u, v, d] = es[i];
cin >> u >> v >> d;
--u, --v;
}
const int src = 0, dst = n - 1;
auto [dag, ord] = shortest_dag(es, src, n);
vector<vector<pair<int,int>>> g(n);
for (auto [x, y, w] : dag) {
debug(x+1, y+1, w);
g[y].emplace_back(x, w);
}
const int lim = n / 4 + 1;
for (int r = 0; r < 4; r++) {
int tot = 0;
for (int i = 0; i < n; i++)
mapping[i] = -1;
for (int x : ord) {
if (x == src) {
mapping[x] = tot++;
debug(mapping[x], tot);
l[mapping[x]] = List();
dep[x] = 0;
continue;
}
cur = List();
cur.add(0, 50001);
pa[x] = -1;
auto GetList = [&](auto self, int z) {
debug("GETLIST", z + 1);
if (dep[z] % 4 == r || z == src) {
debug(z);
tmp = l[mapping[z]];
return;
}
self(self, pa[z]);
tmp.add(paw[z]);
};
for (auto [y, w] : g[x]) {
GetList(GetList, y);
// debug(tmp.sz, cur.sz);
// orange(tmp.e, tmp.e + tmp.sz);
// debug(y+1, w);
tmp.add(w);
// orange(tmp.e, tmp.e + tmp.sz);
if (cmp(tmp, cur)) {
cur = tmp;
pa[x] = y;
paw[x] = w;
} else {
debug(tmp.sz, cur.sz);
orange(tmp.e, tmp.e + tmp.sz);
assert(pa[x] != -1);
}
}
assert(pa[x] != -1);
debug(r);
debug(x+1);
orange(cur.e, cur.e + cur.sz);
dep[x] = dep[pa[x]] + 1;
if (dep[x] % 4 == r) {
mapping[x] = tot++;
if (tot > lim) {
debug(x);
orange(all(ord));
orange(pa, pa + n);
orange(dep, dep + n);
debug("VIOLATED");
break;
}
l[mapping[x]] = cur;
}
if (x == dst) {
vector<int> v;
// v.reserve(n);
for (int i = x; i != src; i = pa[i]) {
v.push_back(i);
}
v.push_back(src);
reverse(all(v));
cout << v.size() << '\n';
for (size_t i = 0; i < v.size(); i++)
cout << v[i] + 1 << (i+1==v.size() ? '\n' : ' ');
return;
}
}
}
assert(false);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 35900kb
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: 214ms
memory: 35440kb
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: 348ms
memory: 61016kb
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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...
result:
ok correct (4 test cases)
Test #4:
score: 0
Accepted
time: 569ms
memory: 58404kb
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 7164 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: 376ms
memory: 56720kb
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 1699 1698 1697 31170 31169 31168 31279 38643 38642 38641 38640 38639 38638 38637 38636 38635 38634 38633 38632 38631 38630 38629 38628 38627 38626 38625 38624 38623 38622 38621 38620 38619 38618 38617 38616 38615 38614 38613 38612 38611 38610 38609 38608 38607 38606 38605 38604 38603 386...
result:
ok correct (4 test cases)