-
Notifications
You must be signed in to change notification settings - Fork 0
/
1334.cpp
191 lines (183 loc) · 5.59 KB
/
1334.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <set>
using namespace std;
// fastest possible dijkstra
// class Solution {
// public:
// typedef vector<vector<pair<int, int>>> graph;
// int k;
// void dfs(int v, graph& g, int* d, char* used) {
// used[v] = true;
// for (int i = 0; i < g[v].size(); ++i) {
// int to = g[v][i].first;
// int cost = g[v][i].second;
// if (d[to] >= d[v] + cost && d[v] + cost <= k) {
// d[to] = d[v] + cost;
// if (!used[to])
// dfs(to, g, d, used);
// }
// }
// }
// int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
// graph g(n, vector<pair<int, int>>(1));
// this->k = distanceThreshold;
// for (int i = 0; i < edges.size(); ++i) {
// int from = edges[i][0];
// int to = edges[i][1];
// int cost = edges[i][2];
// g[from].push_back(make_pair(to, cost));
// g[to].push_back(make_pair(from, cost));
// }
// int ans = 1e9, v = 0;
// for (int i = 0; i < n; ++i) {
// int d[n];
// char used[n];
// memset(d, 63, sizeof(d));
// for (int i = 0; i < n; ++i) {
// cout << d[i] << " ";
// }
// cout << endl;
// memset(used, 0, sizeof(used));
// d[i] = 0;
// dfs(i, g, d, used);
// int count = 0;
// for (int j = 0; j < n; ++j) {
// if (d[j] <= k)
// ++count;
// }
// if (count <= ans) {
// v = i;
// ans = count;
// }
// }
// return ans;
// }
// };
class Solution {
public:
typedef vector<vector<pair<int, int>>> graph;
int k;
int dijkstra(int v, graph& g) {
int n = g.size();
vector<int> d(n, 1e9);
d[v] = 0;
auto comp = [&d](int a, int b) {
return d[a] < d[b];
};
set<int, decltype(comp)> s(comp);
s.insert(v);
while (!s.empty()) {
int v = *s.begin();
s.erase(s.begin());
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i].first;
int cost = g[v][i].second;
if (d[to] > d[v] + cost) {
if (s.find(to) == s.end())
s.erase(to);
d[to] = d[v] + cost;
s.insert(to);
}
}
}
int count = -1;
for (int i = 0; i < n; ++i)
if (d[i] <= k)
++count;
cout << v << " " << count << endl;
return count;
}
int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
graph g(n, vector<pair<int, int>>());
this->k = distanceThreshold;
for (int i = 0; i < edges.size(); ++i) {
int from = edges[i][0];
int to = edges[i][1];
int cost = edges[i][2];
g[from].push_back(make_pair(to, cost));
g[to].push_back(make_pair(from, cost));
}
int mm = 1e9, v = 0;
for (int i = 0; i < n; ++i) {
int count = dijkstra(i, g);
if (count <= mm) {
v = i;
mm = count;
}
}
return v;
}
};
int main() {
int n, k;
cin >> n;
vector<vector<int>> v(n, vector<int>(3));
for (int i = 0; i < n; ++i) {
cin >> v[i][0] >> v[i][1] >> v[i][2];
}
cin >> k;
Solution solution;
int ans = solution.findTheCity(n, v, k);
cout << ans << endl;
return 0;
};
/// using faster Dijkstra
// class Solution {
// public:
// typedef vector<vector<pair<int, int>>> graph;
// int k;
// int dijkstra(int v, graph& g) {
// int n = g.size();
// int d[n];
// memset(d, 63, sizeof(d));
// d[v] = 0;
// set<pair<int, int>> s;
// s.insert(make_pair(d[v], v));
// while (!s.empty()) {
// int v = s.begin()->second;
// s.erase(s.begin());
// for (int i = 0; i < g[v].size(); ++i) {
// int to = g[v][i].first;
// int cost = g[v][i].second;
// if (d[to] > d[v] + cost) {
// s.erase(make_pair(d[to], to));
// d[to] = d[v] + cost;
// s.insert(make_pair(d[to], to));
// }
// }
// }
// int count = -1;
// for (int i = 0; i < n; ++i)
// if (d[i] <= k)
// ++count;
// return count;
// }
// int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
// graph g(n, vector<pair<int, int>>());
// this->k = distanceThreshold;
// for (int i = 0; i < edges.size(); ++i) {
// int from = edges[i][0];
// int to = edges[i][1];
// int cost = edges[i][2];
// g[from].push_back(make_pair(to, cost));
// g[to].push_back(make_pair(from, cost));
// }
// int mm = 1e9, v = 0;
// for (int i = 0; i < n; ++i) {
// int count = dijkstra(i, g);
// if (count <= mm) {
// v = i;
// mm = count;
// }
// }
// return v;
// }
// };