LC.P1334[阈值距离内邻居最少的城市] 方法一:Dijkstra12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455class Solution { private static final int INF = 0x3f3f3f3f; int[][] g; int n; int[] dist; boolean[] visited; int distanceThreshold; public int findTheCity(int n, int[][] edges, int distanceThreshold) { g = new int[n][n]; this.n = n; dist = new int[n]; visited = new boolean[n]; this.distanceThreshold = distanceThreshold; for (int[] e : g) Arrays.fill(e, INF); for (int[] e : edges) { int a = e[0], b = e[1], w = e[2]; g[a][b] = w; g[b][a] = w; } int ans = n, cnt = INF; for (int i = n - 1; i >= 0; --i) { int t = dijkstra(i); if (t < cnt) { cnt = t; ans = i; } } return ans; } private int dijkstra(int u) { Arrays.fill(dist, INF); Arrays.fill(visited, false); dist[u] = 0; for (int i = 0; i < n; ++i) { int k = -1; for (int j = 0; j < n; ++j) { if (!visited[j] && (k == -1 || dist[k] > dist[j])) { k = j; } } visited[k] = true; for (int j = 0; j < n; ++j) { dist[j] = Math.min(dist[j], dist[k] + g[k][j]); } } int cnt = 0; for (int d : dist) { if (d <= distanceThreshold) ++cnt; } return cnt; }} 时间复杂度:$O(n^3)$ 空间复杂度:$O(n^2)$ 方法二:Floyd123456789101112131415161718192021222324252627282930313233343536class Solution { private static final int INF = 0x3f3f3f3f; public int findTheCity(int n, int[][] edges, int distanceThreshold) { int[][] g = new int[n][n]; for (int[] e : g) Arrays.fill(e, INF); for (int[] e : edges) { int a = e[0], b = e[1], w = e[2]; g[a][b] = w; g[b][a] = w; } for (int k = 0; k < n; ++k) { g[k][k] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); } } } int ans = n, cnt = INF; for (int i = n - 1; i >= 0; --i) { int t = 0; for (int d : g[i]) { if (d <= distanceThreshold) { ++t; } } if (t < cnt) { cnt = t; ans = i; } } return ans; }} 时间复杂度:$O(n^3)$ 空间复杂度:$O(n^2)$