-
Notifications
You must be signed in to change notification settings - Fork 2
/
1002 - Country Roads.cpp
126 lines (107 loc) · 2.32 KB
/
1002 - 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<bits/stdc++.h>
using namespace std;
#define mx 501
struct Edge
{
int v;
int cost;
Edge(int _v, int _cost)
{
v = _v;
cost = _cost;
}
};
int mat[mx][mx];
bool operator < (Edge a, Edge b)
{
return a.cost > b.cost;
}
bool comp(Edge a, Edge b)
{
return a.cost<b.cost;
}
int dist[mx];
vector<Edge> adj[mx];
void dijkstra(int s,int n)
{
priority_queue<Edge> PQ;
int vis[n];
for(int i=0; i<n; i++) dist[i] = 10000000, vis[i]=0;
int ans = 10000000;
dist[s] = 0;
vis[s] = 1;
PQ.push(Edge(s,0));
while(!PQ.empty())
{
Edge x = PQ.top();
PQ.pop();
int sz = adj[x.v].size();
for(int i=0; i<sz; i++)
{
Edge e = adj[x.v][i];
int c = dist[e.v] ;
int kk=max(mat[ x.v ][ e.v ], dist[x.v] );
if(kk<dist[e.v])
{
dist[e.v]=kk;
if(vis[e.v] != 1)
{
vis[e.v]=1;
// printf("%d to %d -> updated to %d to %d\n",x.v,e.v,c,dist[e.v]);
PQ.push(Edge(e.v, dist[e.v]));
}
}
// if(e.v==t)
// ans = min(ans,dist[t]);
}
vis[x.v]=0;
}
for(int i=0; i<n; i++){
if(dist[i]==10000000)
printf("Impossible\n");
else
printf("%d\n",dist[i]);
}
}
int main()
{
//freopen("out.txt","w",stdout);
int tc,n,m,u,v,w,source;
scanf("%d",&tc);
for(int t=1; t<=tc; t++)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
mat[i][j] = 10000000;
}
}
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&w);
mat[u][v] = min(w, mat[u][v]);
mat[v][u] = min(w, mat[v][u]);
adj[u].push_back(Edge(v,w));
adj[v].push_back(Edge(u,w));
}
scanf("%d",&source);
printf("Case %d:\n",t);
dijkstra(source,n);
for(int i=0; i<n; i++)
adj[i].clear();
}
return 0;
}
/**
1
3 6
1 2 7335
2 0 13172
0 2 19049
0 1 15649
1 2 10364
1 2 23514
0
*/