QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77096 | #5506. Hyperloop | thomas_li | AC ✓ | 8482ms | 33740kb | C++14 | 7.0kb | 2023-02-13 02:37:38 | 2023-02-13 02:37:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template <class T> struct LinkedListWeightedGraph {
vector<int> HEAD, TO, NXT; vector<T> WEIGHT;
LinkedListWeightedGraph(int V = 0) : HEAD(V, -1) {}
void reserveDiEdges(int maxEdges) {
TO.reserve(maxEdges); NXT.reserve(maxEdges); WEIGHT.reserve(maxEdges);
}
void addDiEdge(int from, int to, T weight) {
NXT.push_back(HEAD[from]); HEAD[from] = int(TO.size());
TO.push_back(to); WEIGHT.push_back(weight);
}
void addBiEdge(int v, int w, T weight) {
addDiEdge(v, w, weight); addDiEdge(w, v, weight);
}
struct Iterator {
const LinkedListWeightedGraph &G; int i;
Iterator(const LinkedListWeightedGraph &G, int i) : G(G), i(i) {}
Iterator &operator ++ () { i = G.NXT[i]; return *this; }
pair<int, T> operator * () const {
return make_pair(G.TO[i], G.WEIGHT[i]);
}
bool operator != (const Iterator &it) const { return i != it.i; }
};
struct Adj {
const LinkedListWeightedGraph &G; int v;
Adj(const LinkedListWeightedGraph &G, int v) : G(G), v(v) {}
const Iterator begin() const { return Iterator(G, G.HEAD[v]); }
const Iterator end() const { return Iterator(G, -1); }
};
const Adj operator [] (int v) const { return Adj(*this, v); }
int size() const { return HEAD.size(); }
void free(){
vector<int>().swap(HEAD);
vector<int>().swap(TO);
vector<int>().swap(NXT);
vector<T>().swap(WEIGHT);
}
};
const int MM = 1e5+5; const int64_t inf = 0x3f3f3f3f3f3f3f3f;
using Gr = LinkedListWeightedGraph<int>;
int n,m; int64_t d1[MM],dn[MM]; int ti; Gr adj,g,rg;
bitset<MM*3> good;
bitset<MM> vis; vector<int> ans,cur;
bool rec(int u){
if(u == n-1){
cur.push_back(n-1);
ans = cur;
return true;
}
if(vis[u]) return false;
cur.push_back(u);
vis[u] = 1;
for(auto[v,t] : g[u]){
if(good[t] && rec(v)) return true;
}
cur.pop_back();
return false;
}
array<int,4> rel[MM*3];
void solve(){
cin >> n >> m;
adj = Gr(n); g = Gr(n); rg = Gr(n);
for(int i = 0; i < n; i++){
d1[i] = dn[i] = inf;
}
ti = 0;
vis.reset(); good.reset();
ans.clear(); cur.clear();
vector<array<int,3>> el(m);
for(int i = 0; i < m; i++){
int u,v,w; cin >> u >> v >> w; u--; v--;
el[i] = {u,v,w};
adj.addDiEdge(u,v,w);
adj.addDiEdge(v,u,w);
}
auto go = [&](int st, int64_t dist[]){
dist[st] = 0; priority_queue<pair<int64_t,int>,vector<pair<int64_t,int>>,greater<>> q;
q.push({0,st});
while(q.size()){
auto[d,u] = q.top(); q.pop();
for(auto[v,w] : adj[u]) if(dist[v] > dist[u]+w){
dist[v] = dist[u]+w;
q.push({dist[v],v});
}
}
};
go(0,d1); go(n-1,dn);
int tot = d1[n-1]; //assert(tot <= 5e4);
// don't need anymore
adj.free();
for(auto[u,v,w]:el){
if(dn[v]+d1[u]+w == tot){
g.addDiEdge(u,v,ti); rg.addDiEdge(v,u,ti);
rel[ti] = {w,u,v,ti};
ti++;
//cout << u << " " << v << " " << w << "\n";
}
swap(u,v);
if(dn[v]+d1[u]+w == tot){
g.addDiEdge(u,v,ti); rg.addDiEdge(v,u,ti);
rel[ti] = {w,u,v,ti};
ti++;
//cout << u << " " << v << " " << w << "\n";
}
}
for(int i = 0; i < ti; i++) good[i] = 1;
sort(rel,rel+ti,greater<>());
vector<int> vals;
for(int i = 0; i < ti; i++){
int j = i;
while(j+1 < ti && rel[j+1][0] == rel[i][0]) j++;
bool fl = 0; bitset<MM*3> we;
for(int k = i; k <= j; k++){
auto[w,u,v,t] = rel[k];
if(good[t]){
fl = 1;
we[t] = 1;
}
}
if(!fl){
i = j;
continue;
}
bitset<MM*3> cur_good;
// go dag dp
auto amog = [&](int st, Gr gr){
vector<int> dp(n),ind(n);
for(int i = 0; i < n; i++){
for(auto[v,t] : gr[i]){
if(!good[t]) continue;
ind[v]++;
}
}
queue<int> q;
for(int i = 0; i < n; i++){
if(ind[i] == 0){
q.push(i);
}
}
while(q.size()){
int u = q.front(); q.pop();
for(auto[v,t] : gr[u]) if(good[t]){
dp[v] = max(dp[v],dp[u]+we[t]);
if(--ind[v] == 0){
q.push(v);
}
}
}
vector<int>().swap(ind);
return dp;
};
auto g1 = amog(0,g), gn = amog(n-1,rg);
int targ = g1[n-1];
for(int k = 0; k < ti; k++){
auto[w,u,v,t] = rel[k];
if(!good[t]) continue;
if(g1[u]+we[t]+gn[v] == targ){
cur_good[t] = 1;
}
}
vector<int>().swap(g1);
vector<int>().swap(gn);
good &= cur_good;
i = j;
}
/*
for(int i = tot; i >= 1; i--){
if(!mp.count(i)) continue;
auto& vec = mp[i];
bool fl = 0; bitset<MM> we;
for(auto[u,v,t]:vec) if(good[t]){
fl = 1; we[t] = 1;
}
if(!fl) continue;
bitset<MM> cur_good;
// go dag dp
auto amog = [&](int st, Gr gr){
vector<int> dp(n+1),ind(n+1);
for(int i = 1; i <= n; i++){
for(auto[v,t] : gr[i]){
if(!good[t]) continue;
ind[v]++;
}
}
queue<int> q;
for(int i = 1; i <= n; i++){
if(ind[i] == 0){
q.push(i);
}
}
while(q.size()){
int u = q.front(); q.pop();
for(auto[v,t] : gr[u]) if(good[t]){
dp[v] = max(dp[v],dp[u]+we[t]);
if(--ind[v] == 0){
q.push(v);
}
}
}
return dp;
};
auto g1 = amog(1,g), gn = amog(n,rg);
int targ = g1[n];
for(auto[u,v,t]:rel){
if(!good[t]) continue;
if(g1[u]+we[t]+gn[v] == targ){
//cout << u << " " << v << " " << t << " " << targ << " " << i << "\n";
cur_good[t] = 1;
}
}
good &= cur_good;
}*/
// generate
/*
for(int i = 1; i <= ti; i++) cout << good[i];
cout << "\n";*/
bool res = rec(0); //assert(res);
cout << ans.size() << "\n";
for(int i = 0; i < (int)ans.size(); i++) cout << ans[i]+1 << " \n"[i==(int)ans.size()-1];
rg.free(); g.free();
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t; while(t--) solve();
}
// min length will be <= 5e4, so only sqrt distinct weights that are useful
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5472kb
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 3 4 5 1 2 5 3 6
result:
ok correct (2 test cases)
Test #2:
score: 0
Accepted
time: 714ms
memory: 5812kb
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 88 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: 798ms
memory: 33740kb
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: 8482ms
memory: 30140kb
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 7171 97126 7163 24798 24799 11787 31031 53551 7309 7310 35482 7933 25067 32714 32715 44194 2068 72216 7959...
result:
ok correct (4 test cases)
Test #5:
score: 0
Accepted
time: 756ms
memory: 27720kb
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 94660 94659 31256 1697 99628 31243 99467 31285 87899 31189 31212 91832 31210 2471 91847 2505 98560 2509 2510 2585 2516 2523 99749 71980 74130 74129 99693 84670 88173 88172 88171 88170 88169 88168 88167 88166 88165 88164 88163 88162 88161 88160 88159 88158 88157 88156 91942 91941 91940 91939 99...
result:
ok correct (4 test cases)