-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59e.cpp
More file actions
86 lines (74 loc) · 1.88 KB
/
Copy path59e.cpp
File metadata and controls
86 lines (74 loc) · 1.88 KB
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
#include <bits/stdc++.h>
#include <queue>
#include <utility>
using namespace std;
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
// dist, loc, prev
struct state {
int dist;
int location;
int prev;
};
bool operator<(const state &first, const state &second) {
return first.dist > second.dist;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m, k;
cin >> n >> m >> k;
vvi edges(n+1);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
map<pii, set<int>> bad;
for (int i = 0; i < k; i++) {
int a, b, c;
cin >> a >> b >> c;
bad[make_pair(a, b)].insert(c);
}
vi dist(n+1, INT_MAX);
vi parent(n+1, -1);
priority_queue<state> pq;
pq.push(state{0, 1, 0});
dist[1] = 0;
while (!pq.empty()) {
state cur = pq.top();
pq.pop();
cout << "at cur=" << cur.location << ", prev=" << cur.prev << endl;
for (int dest : edges[cur.location]) {
if (bad[make_pair(cur.prev, cur.location)].count(dest))
continue;
if (dist[dest] <= cur.dist + 1)
continue;
dist[dest] = cur.dist + 1;
parent[dest] = cur.location;
pq.push(state{cur.dist + 1, dest, cur.location});
}
}
if (dist[n] == INT_MAX) {
cout << -1 << endl;
} else {
vi path;
int cur = n;
while (cur != -1) {
path.push_back(cur);
cur = parent[cur];
}
reverse(path.begin(), path.end());
cout << path.size() - 1 << endl;
for (int x : path) {
cout << x << " ";
}
cout << endl;
}
}