-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountry Roads.cpp
77 lines (66 loc) · 1.42 KB
/
Country Roads.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int inf=1e9+10;
vector<int> vis(N);
vector<int> dist(N,inf);
void dijkstra(int source,vector<pair<int,int>> g[]){
set <pair<int,int>> st;
st.insert({0,source});
dist[source]=0;
while(st.size()>0){
auto node = *st.begin();
int v=node.second;
int dis=node.first;
st.erase(st.begin());
if(vis[v]){
continue;
}
vis[v]=1;
for(auto child:g[v]){
int child_v=child.first;
int wt=child.second;
if(max(dist[v],wt)<dist[child_v]){
dist[child_v]=max(dist[v],wt);
st.insert({dist[child_v],child_v});
}
}
}
}
void solve()
{
vector<pair<int,int>> g[N];
fill(dist.begin(),dist.end(),inf);
fill(vis.begin(),vis.end(),0);
int n,m;
cin>>n>>m;
for(int i=0; i<m; i++)
{
int x,y,wt;
cin>>x>>y>>wt;
g[x].push_back({y,wt});
g[y].push_back({x,wt});
}
int t;
cin>>t;
dijkstra(t,g);
for(int j=0; j<n; j++)
{
if(dist[j]==inf)
cout<<"Impossible\n";
else
cout <<dist[j]<<endl;
}
}
signed main()
{
int t=1;
cin>>t;
for(int i=1;i<=t;i++)
{
// memset( dist, inf ,sizeof(dist));
cout<<"Case "<<i<<":\n";
solve();
}
return 0;
}