-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle_Source_Shortest_Path_Safe_Path.cpp
More file actions
67 lines (50 loc) · 1.86 KB
/
Single_Source_Shortest_Path_Safe_Path.cpp
File metadata and controls
67 lines (50 loc) · 1.86 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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int TC; cin >> TC;
while (TC--) {
int V; cin >> V;
vector<vii> AL(V, vii());
for (int u = 0; u < V; ++u) {
int X; cin >> X; // Edges
while (X--) {
int vertex, weight; cin >> vertex >> weight;
AL[u].emplace_back(vertex, weight);
}
}
int Q; cin >> Q;
while (Q--) {
int s, t, k; cin >> s >> t >> k;
int INF = (int)1e9;
vi dist(V, INF); dist[s] = 0;
vi junctions(V,0); junctions[s] = 1;
for (int i = 0; i < V - 1; i++) {
bool modified = false;
for (int u = 0; u < V; u++) {
if (dist[u] != INF) {
for (auto &[v, w] : AL[u]) {
if (dist[u] + w >= dist[v]) {
continue;
}
int currjunction = 1 + junctions[u];
if (k == 1 && t != s) {
break;
} else if (currjunction < k || v == t) { // process k - 1 edges
dist[v] = dist[u] + w;
junctions[v] = currjunction;
modified = true;
}
}
}
}
if (!modified) break;
}
cout << ((dist[t] != INF) ? dist[t] : -1) << '\n';
}
}
return 0;
}