undirected weighted · threshold = 4
0
1
2
3
all-pairs distance matrix
| from \ to | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 0 | 3 | ∞ | ∞ |
| 1 | 3 | 0 | 1 | 4 |
| 2 | ∞ | 1 | 0 | 1 |
| 3 | ∞ | 4 | 1 | 0 |
Floyd-Warshall + count
▸1dist[i][j] = w (edges), 0 (i==i), ∞ else2for k in 0..n-1:3 for i, j:4 if dist[i][k] + dist[k][j] < dist[i][j]:5 dist[i][j] = dist[i][k] + dist[k][j]6// score cities7for each city i:8 count j with dist[i][j] <= threshold9pick fewest count; tie → largest index
state
- n4
- threshold4
- tie-breaklargest id