-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortestpath1.cpp
60 lines (47 loc) · 1.04 KB
/
shortestpath1.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
#include <iostream>
#include <vector>
#include <set>
#define INF 1000000000
using namespace std;
typedef pair<int, int> ii;
vector<vector<ii> > adjList;
vector<int> dist;
void dijkstra(int source) {
set<ii> solved;
dist[source] = 0;
solved.insert(make_pair(0, source));
while(!solved.empty()) {
ii temp = *(solved.begin());
solved.erase(temp);
int u = temp.second;
for (auto &it : adjList[u]) {
int v = it.first;
int w = it.second;
if (dist[v] > dist[u] + w) {
if (dist[v] != INF) solved.erase(make_pair(dist[v], v));
dist[v] = dist[u] + w;
solved.insert(make_pair(dist[v], v));
}
}
}
}
int main () {
int n, m, q, s;
while (cin >> n >> m >> q >> s, n || m || q || s) {
adjList.assign(n, vector<ii>());
dist.assign(n, INF);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
adjList[u].emplace_back(v, w);
}
dijkstra(s);
while (q--) {
int dest;
cin >> dest;
if (dist[dest] == INF) cout << "Impossible" << endl;
else cout << dist[dest] << endl;
}
}
return 0;
}