QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377818 | #5506. Hyperloop | ckiseki# | ML | 209ms | 46304kb | C++20 | 4.7kb | 2024-04-05 18:20:07 | 2024-04-05 18:20:08 |
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 / 3], 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 / 3 + 2;
for (int r = 0; r < 3; 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] % 3 == 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] % 3 == 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: 4ms
memory: 46304kb
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: 209ms
memory: 45612kb
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: -100
Memory Limit Exceeded
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...