diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md index 16fea24c5faae..20c633d1c9fe2 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md @@ -329,6 +329,50 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n } ``` +#### JavaScript + +```js +function findTheCity(n, edges, distanceThreshold) { + const g = Array.from({ length: n }, () => Array(n).fill(Infinity)); + const dist = Array(n).fill(Infinity); + const vis = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f][t] = g[t][f] = w; + } + + const dijkstra = u => { + dist.fill(Infinity); + vis.fill(false); + dist[u] = 0; + for (let i = 0; i < n; ++i) { + let k = -1; + for (let j = 0; j < n; ++j) { + if (!vis[j] && (k === -1 || dist[j] < dist[k])) { + k = j; + } + } + vis[k] = true; + for (let j = 0; j < n; ++j) { + dist[j] = Math.min(dist[j], dist[k] + g[k][j]); + } + } + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = Infinity; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} +``` + @@ -522,6 +566,152 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n } ``` +#### JavaScript + +```js +function findTheCity(n, edges, distanceThreshold) { + const g = Array.from({ length: n }, () => Array(n).fill(Infinity)); + for (const [f, t, w] of edges) { + g[f][t] = g[t][f] = w; + } + for (let k = 0; k < n; ++k) { + g[k][k] = 0; + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n; ++j) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } + } + } + + let ans = n, + cnt = n + 1; + for (let i = n - 1; i >= 0; --i) { + const t = g[i].filter(x => x <= distanceThreshold).length; + if (t < cnt) { + cnt = t; + ans = i; + } + } + return ans; +} +``` + + + + + + + +### Solution 3 + + + +#### TypeScript + +```ts +function findTheCity(n: number, edges: number[][], distanceThreshold: number): number { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + const dist: number[] = Array(n).fill(MAX); + const vis: boolean[] = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f].set(t, w); + g[t].set(f, w); + } + + const dijkstra = (u: number): number => { + dist.fill(MAX); + vis.fill(false); + dist[u] = 0; + const pq = new MinPriorityQueue(); + pq.enqueue(u, 0); + + while (!pq.isEmpty()) { + const u = pq.dequeue().element; + if (vis[u]) continue; + vis[u] = true; + + for (const [v, w] of g[u]) { + if (vis[v]) continue; + + const wNext = dist[u] + w; + if (wNext < dist[v]) { + dist[v] = wNext; + pq.enqueue(v, dist[v]); + } + } + } + + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = MAX; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} +``` + +#### JavaScript + +```js +export function findTheCity(n, edges, distanceThreshold) { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + const dist = Array(n).fill(MAX); + const vis = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f].set(t, w); + g[t].set(f, w); + } + + const dijkstra = u => { + dist.fill(MAX); + vis.fill(false); + dist[u] = 0; + const pq = new MinPriorityQueue(); + pq.enqueue(u, 0); + + while (!pq.isEmpty()) { + const u = pq.dequeue().element; + if (vis[u]) continue; + vis[u] = true; + + for (const [v, w] of g[u]) { + if (vis[v]) continue; + + const wNext = dist[u] + w; + if (wNext < dist[v]) { + dist[v] = wNext; + pq.enqueue(v, dist[v]); + } + } + } + + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = MAX; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md index 20d960a5eaa5f..283121786a480 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md @@ -319,6 +319,50 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n } ``` +#### JavaScript + +```js +function findTheCity(n, edges, distanceThreshold) { + const g = Array.from({ length: n }, () => Array(n).fill(Infinity)); + const dist = Array(n).fill(Infinity); + const vis = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f][t] = g[t][f] = w; + } + + const dijkstra = u => { + dist.fill(Infinity); + vis.fill(false); + dist[u] = 0; + for (let i = 0; i < n; ++i) { + let k = -1; + for (let j = 0; j < n; ++j) { + if (!vis[j] && (k === -1 || dist[j] < dist[k])) { + k = j; + } + } + vis[k] = true; + for (let j = 0; j < n; ++j) { + dist[j] = Math.min(dist[j], dist[k] + g[k][j]); + } + } + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = Infinity; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} +``` + @@ -504,6 +548,152 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n } ``` +#### JavaScript + +```js +function findTheCity(n, edges, distanceThreshold) { + const g = Array.from({ length: n }, () => Array(n).fill(Infinity)); + for (const [f, t, w] of edges) { + g[f][t] = g[t][f] = w; + } + for (let k = 0; k < n; ++k) { + g[k][k] = 0; + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n; ++j) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } + } + } + + let ans = n, + cnt = n + 1; + for (let i = n - 1; i >= 0; --i) { + const t = g[i].filter(x => x <= distanceThreshold).length; + if (t < cnt) { + cnt = t; + ans = i; + } + } + return ans; +} +``` + + + + + + + +### Solution 3 + + + +#### TypeScript + +```ts +function findTheCity(n: number, edges: number[][], distanceThreshold: number): number { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + const dist: number[] = Array(n).fill(MAX); + const vis: boolean[] = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f].set(t, w); + g[t].set(f, w); + } + + const dijkstra = (u: number): number => { + dist.fill(MAX); + vis.fill(false); + dist[u] = 0; + const pq = new MinPriorityQueue(); + pq.enqueue(u, 0); + + while (!pq.isEmpty()) { + const u = pq.dequeue().element; + if (vis[u]) continue; + vis[u] = true; + + for (const [v, w] of g[u]) { + if (vis[v]) continue; + + const wNext = dist[u] + w; + if (wNext < dist[v]) { + dist[v] = wNext; + pq.enqueue(v, dist[v]); + } + } + } + + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = MAX; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} +``` + +#### JavaScript + +```js +export function findTheCity(n, edges, distanceThreshold) { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + const dist = Array(n).fill(MAX); + const vis = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f].set(t, w); + g[t].set(f, w); + } + + const dijkstra = u => { + dist.fill(MAX); + vis.fill(false); + dist[u] = 0; + const pq = new MinPriorityQueue(); + pq.enqueue(u, 0); + + while (!pq.isEmpty()) { + const u = pq.dequeue().element; + if (vis[u]) continue; + vis[u] = true; + + for (const [v, w] of g[u]) { + if (vis[v]) continue; + + const wNext = dist[u] + w; + if (wNext < dist[v]) { + dist[v] = wNext; + pq.enqueue(v, dist[v]); + } + } + } + + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = MAX; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js new file mode 100644 index 0000000000000..df5b8032bf103 --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js @@ -0,0 +1,39 @@ +function findTheCity(n, edges, distanceThreshold) { + const g = Array.from({ length: n }, () => Array(n).fill(Infinity)); + const dist = Array(n).fill(Infinity); + const vis = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f][t] = g[t][f] = w; + } + + const dijkstra = u => { + dist.fill(Infinity); + vis.fill(false); + dist[u] = 0; + for (let i = 0; i < n; ++i) { + let k = -1; + for (let j = 0; j < n; ++j) { + if (!vis[j] && (k === -1 || dist[j] < dist[k])) { + k = j; + } + } + vis[k] = true; + for (let j = 0; j < n; ++j) { + dist[j] = Math.min(dist[j], dist[k] + g[k][j]); + } + } + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = Infinity; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js new file mode 100644 index 0000000000000..eefee56d3ee4e --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js @@ -0,0 +1,25 @@ +function findTheCity(n, edges, distanceThreshold) { + const g = Array.from({ length: n }, () => Array(n).fill(Infinity)); + for (const [f, t, w] of edges) { + g[f][t] = g[t][f] = w; + } + for (let k = 0; k < n; ++k) { + g[k][k] = 0; + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n; ++j) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } + } + } + + let ans = n, + cnt = n + 1; + for (let i = n - 1; i >= 0; --i) { + const t = g[i].filter(x => x <= distanceThreshold).length; + if (t < cnt) { + cnt = t; + ans = i; + } + } + return ans; +} diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js new file mode 100644 index 0000000000000..1ed22e36f6c56 --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js @@ -0,0 +1,48 @@ +export function findTheCity(n, edges, distanceThreshold) { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + const dist = Array(n).fill(MAX); + const vis = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f].set(t, w); + g[t].set(f, w); + } + + const dijkstra = u => { + dist.fill(MAX); + vis.fill(false); + dist[u] = 0; + const pq = new MinPriorityQueue(); + pq.enqueue(u, 0); + + while (!pq.isEmpty()) { + const u = pq.dequeue().element; + if (vis[u]) continue; + vis[u] = true; + + for (const [v, w] of g[u]) { + if (vis[v]) continue; + + const wNext = dist[u] + w; + if (wNext < dist[v]) { + dist[v] = wNext; + pq.enqueue(v, dist[v]); + } + } + } + + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = MAX; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +} diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts new file mode 100644 index 0000000000000..cd8545d7a82c9 --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts @@ -0,0 +1,48 @@ +function findTheCity(n: number, edges: number[][], distanceThreshold: number): number { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + const dist: number[] = Array(n).fill(MAX); + const vis: boolean[] = Array(n).fill(false); + for (const [f, t, w] of edges) { + g[f].set(t, w); + g[t].set(f, w); + } + + const dijkstra = (u: number): number => { + dist.fill(MAX); + vis.fill(false); + dist[u] = 0; + const pq = new MinPriorityQueue(); + pq.enqueue(u, 0); + + while (!pq.isEmpty()) { + const u = pq.dequeue().element; + if (vis[u]) continue; + vis[u] = true; + + for (const [v, w] of g[u]) { + if (vis[v]) continue; + + const wNext = dist[u] + w; + if (wNext < dist[v]) { + dist[v] = wNext; + pq.enqueue(v, dist[v]); + } + } + } + + return dist.filter(d => d <= distanceThreshold).length; + }; + + let ans = n; + let cnt = MAX; + for (let i = n - 1; i >= 0; --i) { + const t = dijkstra(i); + if (t < cnt) { + cnt = t; + ans = i; + } + } + + return ans; +}