-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·68 lines (52 loc) · 1.31 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·68 lines (52 loc) · 1.31 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
bool first = true;
while (true)
{
int n_verts, n_edges, queries, u;
cin >> n_verts >> n_edges >> queries >> u;
if (n_verts == 0 && n_edges == 0 && queries == 0 && u == 0)
break;
if (!first)
cout << endl;
first = false;
vector<vector<pair<int, int>>> adj(n_verts);
vector<int> dist(n_verts, INT32_MAX);
for (int i = 0; i < n_edges; ++i)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
}
dist[u] = 0;
queue<int> Q;
Q.push(u);
while (!Q.empty())
{
u = Q.front();
Q.pop();
for (auto each : adj[u])
{
int v = each.first;
int weight = each.second;
if (dist[u] + weight < dist[v])
{
dist[v] = dist[u] + weight;
Q.push(v);
}
}
}
while (queries--)
{
int v;
cin >> v;
if (dist[v] == INT32_MAX)
cout << "Impossible" << endl;
else
cout << dist[v] << endl;
}
}
return 0;
}