forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetour.cpp
95 lines (79 loc) · 1.93 KB
/
detour.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = (ll)1<<60;
struct edge {
ll n1, n2, w;
};
int main() {
ll n, m;
cin >> n >> m;
vector<vector<pair<ll,ll>>> adj(n);
vector<edge> edges;
for(ll i = 0; i < m; i++) {
ll n1, n2, w;
cin >> n1 >> n2 >> w;
adj[n1].push_back({n2,w});
adj[n2].push_back({n1,w});
edges.push_back({n1,n2,w});
}
vector<ll> d(n,inf);
set<pair<ll,ll>> q;
q.insert({0,1});
d[1] = 0;
while(!q.empty()) {
ll curr = q.begin()->second;
q.erase(q.begin());
for(auto i : adj[curr]) {
ll next = i.first;
ll weight = i.second;
if(d[next] > d[curr] + weight) {
q.erase({d[next],next});
d[next] = d[curr] + weight;
q.insert({d[next],next});
}
}
}
vector<vector<ll>> adj2(n);
for(auto i : edges) {
if(d[i.n1] + i.w == d[i.n2]) continue;
adj2[i.n2].push_back(i.n1);
}
for(auto i : edges) {
if(d[i.n2] + i.w == d[i.n1]) continue;
adj2[i.n1].push_back(i.n2);
}
queue<ll> bq;
bq.push(0);
vector<bool> vis(n,false);
vector<ll> par(n,-1);
vis[0] = true;
while(!bq.empty()) {
ll curr = bq.front();
bq.pop();
for(auto next : adj2[curr]) {
if(!vis[next]) {
bq.push(next);
par[next] = curr;
vis[next] = true;
}
}
}
if(par[1] == -1) {
cout << "impossible" << endl;
}
else {
vector<ll> path;
ll temp = 1;
while(par[temp] != -1) {
path.push_back(temp);
temp = par[temp];
}
path.push_back(temp);
cout << path.size() << " ";
for(ll i = path.size()-1; i >= 0; i--) {
cout << path[i] << " ";
}
cout << endl;
}
}