-
Notifications
You must be signed in to change notification settings - Fork 34
/
1002-Country-Roads.cpp
136 lines (99 loc) · 1.8 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
127
128
129
130
131
132
133
134
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
#include <limits.h>
#include <fstream>
#define ii pair<int, int>
using namespace std;
int dijkstra(vector < vector <pair <int, int > > > x, int n, int my){
pair <int, int> temp;
vector <int> dis(n, INT_MAX);
priority_queue < ii, vector <ii>, greater <ii> > q;
int bro;
temp.first = 0;
temp.second = my;
ii top;
q.push(temp);
int distance;
int index;
int d;
int j;
while (!q.empty()) {
top = q.top();
q.pop();
index = top.second;
distance = top.first;
if(distance <= dis[index]) {
dis[index] = distance;
for (int i = 0; i < x[index].size(); i++) {
temp = x[index][i];
d = temp.first;
j = temp.second;
bro = max(d,distance);
if(bro < dis[j]) {
dis[j] = bro;
q.push(ii(dis[j], j));
}
}
}
}
for (int i = 0; i < n; i++) {
if(dis[i] == INT_MAX) {
cout << "Impossible\n";
}
else {
cout << dis[i] << endl;
}
}
}
int main()
{
int a;
int b;
int w;
int m;
int n;
int t;
int k;
int my;
pair <int, int> temp;
//fstream cin;
//cin.open("1002.in", ios::in);
cin >> t;
for (int j = 1; j <= t; j++) {
cin >> n;
vector < vector <pair <int, int> > > x(n);
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> a;
cin >> b;
cin >> w;
temp.first = w;
temp.second = b;
int k;
int kk;
for(k = 0; k < x[a].size(); k++) {
if(x[a][k].second == b)
break;
}
for(kk = 0; kk < x[b].size(); kk++) {
if(x[b][kk].second == a)
break;
}
if(k == x[a].size()) {
x[a].push_back(temp);
x[b].push_back(ii(w, a));
}
else {
if(temp.first < x[a][k].first ) {
x[a][k] = temp;
x[b][kk] = ii(w, a);
}
}
}
cin >> my;
cout << "Case "<<j<<":"<<endl;
dijkstra(x, n, my);
}
}