|
64 | 64 |
|
65 | 65 | <!-- 这里可写通用的实现逻辑 -->
|
66 | 66 |
|
| 67 | +**方法一:Dijkstra 算法** |
| 68 | + |
| 69 | +我们可以枚举每个城市 $i$ 作为起点,使用 Dijkstra 算法求出从 $i$ 到其他城市的最短距离,然后统计距离不超过阈值的城市个数,最后取最小的个数且编号最大的城市。 |
| 70 | + |
| 71 | +时间复杂度 $O(n^3)$,空间复杂度 $O(n^2)$。其中 $n$ 为城市个数。 |
| 72 | + |
67 | 73 | <!-- tabs:start -->
|
68 | 74 |
|
69 | 75 | ### **Python3**
|
70 | 76 |
|
71 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
72 | 78 |
|
73 | 79 | ```python
|
| 80 | +class Solution: |
| 81 | + def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int: |
| 82 | + def dijkstra(u): |
| 83 | + dist = [inf] * n |
| 84 | + dist[u] = 0 |
| 85 | + vis = [False] * n |
| 86 | + for _ in range(n): |
| 87 | + k = -1 |
| 88 | + for j in range(n): |
| 89 | + if not vis[j] and (k == -1 or dist[k] > dist[j]): |
| 90 | + k = j |
| 91 | + vis[k] = True |
| 92 | + for j in range(n): |
| 93 | + dist[j] = min(dist[j], dist[k] + g[k][j]) |
| 94 | + return sum(d <= distanceThreshold for d in dist) |
| 95 | + |
| 96 | + g = [[inf] * n for _ in range(n)] |
| 97 | + for f, t, w in edges: |
| 98 | + g[f][t] = g[t][f] = w |
74 | 99 |
|
| 100 | + ans = n |
| 101 | + t = inf |
| 102 | + for i in range(n - 1, -1, -1): |
| 103 | + if (cnt := dijkstra(i)) < t: |
| 104 | + t = cnt |
| 105 | + ans = i |
| 106 | + return ans |
75 | 107 | ```
|
76 | 108 |
|
77 | 109 | ### **Java**
|
78 | 110 |
|
79 | 111 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
80 | 112 |
|
81 | 113 | ```java
|
| 114 | +class Solution { |
| 115 | + private int n; |
| 116 | + private int[][] g; |
| 117 | + private int[] dist; |
| 118 | + private boolean[] vis; |
| 119 | + private int inf = 1 << 30; |
| 120 | + private int distanceThreshold; |
| 121 | + |
| 122 | + public int findTheCity(int n, int[][] edges, int distanceThreshold) { |
| 123 | + this.n = n; |
| 124 | + this.distanceThreshold = distanceThreshold; |
| 125 | + g = new int[n][n]; |
| 126 | + dist = new int[n]; |
| 127 | + vis = new boolean[n]; |
| 128 | + for (var e : g) { |
| 129 | + Arrays.fill(e, inf); |
| 130 | + } |
| 131 | + for (var e : edges) { |
| 132 | + int f = e[0], t = e[1], w = e[2]; |
| 133 | + g[f][t] = w; |
| 134 | + g[t][f] = w; |
| 135 | + } |
| 136 | + int ans = n, t = inf; |
| 137 | + for (int i = n - 1; i >= 0; --i) { |
| 138 | + int cnt = dijkstra(i); |
| 139 | + if (t > cnt) { |
| 140 | + t = cnt; |
| 141 | + ans = i; |
| 142 | + } |
| 143 | + } |
| 144 | + return ans; |
| 145 | + } |
| 146 | + |
| 147 | + private int dijkstra(int u) { |
| 148 | + Arrays.fill(dist, inf); |
| 149 | + Arrays.fill(vis, false); |
| 150 | + dist[u] = 0; |
| 151 | + for (int i = 0; i < n; ++i) { |
| 152 | + int k = -1; |
| 153 | + for (int j = 0; j < n; ++j) { |
| 154 | + if (!vis[j] && (k == -1 || dist[k] > dist[j])) { |
| 155 | + k = j; |
| 156 | + } |
| 157 | + } |
| 158 | + vis[k] = true; |
| 159 | + for (int j = 0; j < n; ++j) { |
| 160 | + dist[j] = Math.min(dist[j], dist[k] + g[k][j]); |
| 161 | + } |
| 162 | + } |
| 163 | + int cnt = 0; |
| 164 | + for (int d : dist) { |
| 165 | + if (d <= distanceThreshold) { |
| 166 | + ++cnt; |
| 167 | + } |
| 168 | + } |
| 169 | + return cnt; |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### **C++** |
| 175 | + |
| 176 | +```cpp |
| 177 | +class Solution { |
| 178 | +public: |
| 179 | + int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) { |
| 180 | + const int inf = 1e7; |
| 181 | + vector<vector<int>> g(n, vector<int>(n, inf)); |
| 182 | + vector<int> dist(n, inf); |
| 183 | + vector<bool> vis(n); |
| 184 | + for (auto& e : edges) { |
| 185 | + int f = e[0], t = e[1], w = e[2]; |
| 186 | + g[f][t] = g[t][f] = w; |
| 187 | + } |
| 188 | + auto dijkstra = [&](int u) { |
| 189 | + dist.assign(n, inf); |
| 190 | + vis.assign(n, false); |
| 191 | + dist[u] = 0; |
| 192 | + for (int i = 0; i < n; ++i) { |
| 193 | + int k = -1; |
| 194 | + for (int j = 0; j < n; ++j) { |
| 195 | + if (!vis[j] && (k == -1 || dist[j] < dist[k])) { |
| 196 | + k = j; |
| 197 | + } |
| 198 | + } |
| 199 | + vis[k] = true; |
| 200 | + for (int j = 0; j < n; ++j) { |
| 201 | + dist[j] = min(dist[j], dist[k] + g[k][j]); |
| 202 | + } |
| 203 | + } |
| 204 | + int cnt = 0; |
| 205 | + for (int& d : dist) { |
| 206 | + cnt += d <= distanceThreshold; |
| 207 | + } |
| 208 | + return cnt; |
| 209 | + }; |
| 210 | + int ans = n, t = inf; |
| 211 | + for (int i = n - 1; ~i; --i) { |
| 212 | + int cnt = dijkstra(i); |
| 213 | + if (t > cnt) { |
| 214 | + t = cnt; |
| 215 | + ans = i; |
| 216 | + } |
| 217 | + } |
| 218 | + return ans; |
| 219 | + } |
| 220 | +}; |
| 221 | +``` |
| 222 | +
|
| 223 | +### **Go** |
| 224 | +
|
| 225 | +```go |
| 226 | +func findTheCity(n int, edges [][]int, distanceThreshold int) int { |
| 227 | + g := make([][]int, n) |
| 228 | + dist := make([]int, n) |
| 229 | + vis := make([]bool, n) |
| 230 | + const inf int = 1e7 |
| 231 | + for i := range g { |
| 232 | + g[i] = make([]int, n) |
| 233 | + for j := range g[i] { |
| 234 | + g[i][j] = inf |
| 235 | + } |
| 236 | + } |
| 237 | + for _, e := range edges { |
| 238 | + f, t, w := e[0], e[1], e[2] |
| 239 | + g[f][t], g[t][f] = w, w |
| 240 | + } |
| 241 | +
|
| 242 | + ans, t := n, inf |
| 243 | + dijkstra := func(u int) (cnt int) { |
| 244 | + for i := range vis { |
| 245 | + vis[i] = false |
| 246 | + dist[i] = inf |
| 247 | + } |
| 248 | + dist[u] = 0 |
| 249 | + for i := 0; i < n; i++ { |
| 250 | + k := -1 |
| 251 | + for j := 0; j < n; j++ { |
| 252 | + if !vis[j] && (k == -1 || dist[j] < dist[k]) { |
| 253 | + k = j |
| 254 | + } |
| 255 | + } |
| 256 | + vis[k] = true |
| 257 | + for j := 0; j < n; j++ { |
| 258 | + dist[j] = min(dist[j], dist[k]+g[k][j]) |
| 259 | + } |
| 260 | + } |
| 261 | + for _, d := range dist { |
| 262 | + if d <= distanceThreshold { |
| 263 | + cnt++ |
| 264 | + } |
| 265 | + } |
| 266 | + return |
| 267 | + } |
| 268 | + for i := n - 1; i >= 0; i-- { |
| 269 | + cnt := dijkstra(i) |
| 270 | + if t > cnt { |
| 271 | + t = cnt |
| 272 | + ans = i |
| 273 | + } |
| 274 | + } |
| 275 | + return ans |
| 276 | +} |
82 | 277 |
|
| 278 | +func min(a, b int) int { |
| 279 | + if a < b { |
| 280 | + return a |
| 281 | + } |
| 282 | + return b |
| 283 | +} |
83 | 284 | ```
|
84 | 285 |
|
85 | 286 | ### **...**
|
|
0 commit comments