forked from maiquynhtruong/algorithms-and-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva11280-flying-to-Fredericton.cpp
52 lines (51 loc) · 1.05 KB
/
uva11280-flying-to-Fredericton.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
#include <bits/stdc++.h>
using namespace std;
struct edge {
int u, v, c;
edge (int uu, int vv, int cc) {
u = uu, v = vv, c = cc;
}
};
void findRoute() {
for (int i = 0; i <= N; i++) {
dist[i] = INF;
minCost[i] = INF;
}
dist[1] = 0;
for (int relax = 1; relax < N; relax++) {
for (int i = 0; i < edges.size(); i++) {
int u = edges[i].u, v = edges[i].v, c = edges[i].c;
if (dist[u] != INF && dist[v] > dist[u] + c) {
dist[v] = dist[u] + c;
}
}
minCost[relax] = dist[N];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> N;
cin.ignore();
for (int i = 1; i <= N; i++) {
cin >> city;
cities[city] = i;
}
cin >> M;
for (int i = 0; i < M; i++) {
cin >> from >> to >> cost;
edges[i].u = cities[from];
edges[i].v = cities[to];
edges[i].c = cost;
}
findRoute();
cin >> Q;
for (int i = 0; i < Q; i++) {
cin >> query;
if (minCost[query] == INF) cout << "No satisfactory flights\n";
else cout << "Total cost of flight(s) is " << minCost[query] << "\n";
}
}
}