QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77065 | #5506. Hyperloop | thomas_li | WA | 7ms | 10420kb | C++14 | 3.2kb | 2023-02-13 01:05:08 | 2023-02-13 01:05:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MM = 1e5+5,inf = 0x3f3f3f3f;
int n,m; int d1[MM],dn[MM]; vector<pair<int,int>> adj[MM]; vector<int> g[MM],rg[MM];
bitset<MM> good,vis; vector<int> ans,cur;
bool rec(int u){
if(u == n){
cur.push_back(n);
ans = cur;
return true;
}
if(vis[u]) return false;
cur.push_back(u);
vis[u] = 1;
for(int v : g[u]){
if(good[v] && rec(v)) return true;
}
cur.pop_back();
return false;
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
d1[i] = dn[i] = inf;
g[i].clear(); rg[i].clear();
adj[i].clear();
}
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;
el[i] = {u,v,w};
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
auto go = [&](int st, int dist[]){
dist[st] = 0; priority_queue<pair<int,int>,vector<pair<int,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(1,d1); go(n,dn);
int tot = d1[n];
unordered_map<int,vector<array<int,2>>> mp;
for(auto[u,v,w]:el){
if(dn[v]+d1[u]+w == tot){
g[u].push_back(v); rg[v].push_back(u);
mp[w].push_back({u,v});
}
swap(u,v);
if(dn[v]+d1[u]+w == tot){
g[u].push_back(v); rg[v].push_back(u);
mp[w].push_back({u,v});
}
}
for(int i = 1; i <= n; i++) good[i] = 1;
for(int i = tot; i >= 1; i--){
if(!mp.count(i)) continue;
auto& vec = mp[i]; bitset<MM> cur;
// push low
queue<int> q; bool fl = 0;
for(auto[u,v] : vec){
if(!good[u] || !good[v]) continue;
q.push(v); cur[v] = 1; fl = 1;
}
if(!fl) continue;
while(q.size()){
int u = q.front(); q.pop();
for(int v : g[u]) if(!cur[v] && good[v]){
cur[v] = 1;
q.push(v);
}
}
for(auto[u,v] : vec){
if(!good[u] || !good[v]) continue;
q.push(u); cur[u] = 1;
}
while(q.size()){
int u = q.front(); q.pop();
for(int v : rg[u]) if(!cur[v] && good[v]){
cur[v] = 1;
q.push(v);
}
}
/*
cout << "for " << i << "\n";
for(int i = 1; i <= n; i++){
cout << cur[i];
}
cout << "\n";*/
good &= cur;
}
// generate
/*
for(int i = 1; i <= n; i++) cout << good[i];
cout << "\n";*/
assert(good[1]);
rec(1);
cout << ans.size() << "\n";
for(int i = 0; i < (int)ans.size(); i++) cout << ans[i] << " \n"[i==(int)ans.size()-1];
}
int 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: 0
Wrong Answer
time: 7ms
memory: 10420kb
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:
4 1 2 3 4 5 1 2 5 3 6
result:
wrong answer Contestant's path is not optimal lexicographically (test case 1)