funcnetworkDelayTime(times [][]int, n, k int) (ans int) { const inf = math.MaxInt64 / 2 g := make([][]int, n) for i := range g { g[i] = make([]int, n) for j := range g[i] { g[i][j] = inf } } for _, t := range times { x, y := t[0]-1, t[1]-1 g[x][y] = t[2] }
dist := make([]int, n) for i := range dist { dist[i] = inf } dist[k-1] = 0 used := make([]bool, n) for i := 0; i < n; i++ { x := -1 for y, u := range used { if !u && (x == -1 || dist[y] < dist[x]) { x = y } } used[x] = true for y, time := range g[x] { dist[y] = min(dist[y], dist[x]+time) } }
for _, d := range dist { if d == inf { return-1 } ans = max(ans, d) } return }
funcmin(a, b int)int { if a < b { return a } return b }
funcmax(a, b int)int { if a > b { return a } return b }
classSolution: defnetworkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: g = [[float('inf')] * n for _ inrange(n)] for x, y, time in times: g[x - 1][y - 1] = time
dist = [float('inf')] * n dist[k - 1] = 0 used = [False] * n for _ inrange(n): x = -1 for y, u inenumerate(used): ifnot u and (x == -1or dist[y] < dist[x]): x = y used[x] = True for y, time inenumerate(g[x]): dist[y] = min(dist[y], dist[x] + time)
ans = max(dist) return ans if ans < float('inf') else -1
var networkDelayTime = function(times, n, k) { constINF = Number.MAX_SAFE_INTEGER; const g = newArray(n).fill(INF).map(() =>newArray(n).fill(INF)); for (const t of times) { const x = t[0] - 1, y = t[1] - 1; g[x][y] = t[2]; }
const dist = newArray(n).fill(INF); dist[k - 1] = 0; const used = newArray(n).fill(false); for (let i = 0; i < n; ++i) { let x = -1; for (let y = 0; y < n; ++y) { if (!used[y] && (x === -1 || dist[y] < dist[x])) { x = y; } } used[x] = true; for (let y = 0; y < n; ++y) { dist[y] = Math.min(dist[y], dist[x] + g[x][y]); } }
let ans = Math.max(...dist); return ans === INF ? -1 : ans; };
intnetworkDelayTime(int** times, int timesSize, int* timesColSize, int n, int k) { constint inf = 0x3f3f3f3f; int g[n][n]; memset(g, 0x3f, sizeof(g)); for (int i = 0; i < timesSize; i++) { int x = times[i][0] - 1, y = times[i][1] - 1; g[x][y] = times[i][2]; }
int dist[n]; memset(dist, 0x3f, sizeof(dist)); dist[k - 1] = 0; int used[n]; memset(used, 0, sizeof(used)); int ans = 0; for (int i = 0; i < n; ++i) { int x = -1; for (int y = 0; y < n; ++y) { if (!used[y] && (x == -1 || dist[y] < dist[x])) { x = y; } } used[x] = true; ans = fmax(ans, dist[x]); for (int y = 0; y < n; ++y) { dist[y] = fmin(dist[y], dist[x] + g[x][y]); } }
funcnetworkDelayTime(times [][]int, n, k int) (ans int) { type edge struct{ to, time int } g := make([][]edge, n) for _, t := range times { x, y := t[0]-1, t[1]-1 g[x] = append(g[x], edge{y, t[2]}) }
const inf int = math.MaxInt64 / 2 dist := make([]int, n) for i := range dist { dist[i] = inf } dist[k-1] = 0 h := &hp{{0, k - 1}} for h.Len() > 0 { p := heap.Pop(h).(pair) x := p.x if dist[x] < p.d { continue } for _, e := range g[x] { y := e.to if d := dist[x] + e.time; d < dist[y] { dist[y] = d heap.Push(h, pair{d, y}) } } }
for _, d := range dist { if d == inf { return-1 } ans = max(ans, d) } return }
classSolution: defnetworkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: g = [[] for _ inrange(n)] for x, y, time in times: g[x - 1].append((y - 1, time))
dist = [float('inf')] * n dist[k - 1] = 0 q = [(0, k - 1)] while q: time, x = heapq.heappop(q) if dist[x] < time: continue for y, time in g[x]: if (d := dist[x] + time) < dist[y]: dist[y] = d heapq.heappush(q, (d, y))
ans = max(dist) return ans if ans < float('inf') else -1