Skip to content

Commit 455338f

Browse files
authored
feat: add solutions to lc problem: No.0743 (#3811)
No.0743.Network Delay Time
1 parent ac64037 commit 455338f

File tree

21 files changed

+536
-1103
lines changed

21 files changed

+536
-1103
lines changed

solution/0700-0799/0743.Network Delay Time/README.md

+180-383
Large diffs are not rendered by default.

solution/0700-0799/0743.Network Delay Time/README_EN.md

+184-381
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public:
3-
const int inf = 0x3f3f;
4-
53
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
4+
const int inf = 1 << 29;
65
vector<vector<int>> g(n, vector<int>(n, inf));
7-
for (auto& t : times) g[t[0] - 1][t[1] - 1] = t[2];
8-
vector<bool> vis(n);
6+
for (const auto& e : times) {
7+
g[e[0] - 1][e[1] - 1] = e[2];
8+
}
99
vector<int> dist(n, inf);
1010
dist[k - 1] = 0;
11+
vector<bool> vis(n);
1112
for (int i = 0; i < n; ++i) {
1213
int t = -1;
1314
for (int j = 0; j < n; ++j) {
@@ -20,7 +21,7 @@ class Solution {
2021
dist[j] = min(dist[j], dist[t] + g[t][j]);
2122
}
2223
}
23-
int ans = *max_element(dist.begin(), dist.end());
24+
int ans = ranges::max(dist);
2425
return ans == inf ? -1 : ans;
2526
}
26-
};
27+
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
func networkDelayTime(times [][]int, n int, k int) int {
2-
const inf = 0x3f3f
3-
dist := make([]int, n)
4-
vis := make([]bool, n)
2+
const inf = 1 << 29
53
g := make([][]int, n)
6-
for i := range dist {
7-
dist[i] = inf
4+
for i := range g {
85
g[i] = make([]int, n)
96
for j := range g[i] {
107
g[i][j] = inf
118
}
129
}
13-
for _, t := range times {
14-
g[t[0]-1][t[1]-1] = t[2]
10+
for _, e := range times {
11+
g[e[0]-1][e[1]-1] = e[2]
12+
}
13+
14+
dist := make([]int, n)
15+
for i := range dist {
16+
dist[i] = inf
1517
}
1618
dist[k-1] = 0
19+
20+
vis := make([]bool, n)
1721
for i := 0; i < n; i++ {
1822
t := -1
1923
for j := 0; j < n; j++ {
@@ -26,9 +30,9 @@ func networkDelayTime(times [][]int, n int, k int) int {
2630
dist[j] = min(dist[j], dist[t]+g[t][j])
2731
}
2832
}
29-
ans := slices.Max(dist)
30-
if ans == inf {
31-
return -1
33+
34+
if ans := slices.Max(dist); ans != inf {
35+
return ans
3236
}
33-
return ans
34-
}
37+
return -1
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
2-
private static final int INF = 0x3f3f;
3-
42
public int networkDelayTime(int[][] times, int n, int k) {
53
int[][] g = new int[n][n];
64
int[] dist = new int[n];
7-
boolean[] vis = new boolean[n];
8-
for (int i = 0; i < n; ++i) {
9-
dist[i] = INF;
10-
Arrays.fill(g[i], INF);
5+
final int inf = 1 << 29;
6+
Arrays.fill(dist, inf);
7+
for (var e : g) {
8+
Arrays.fill(e, inf);
119
}
12-
for (int[] t : times) {
13-
g[t[0] - 1][t[1] - 1] = t[2];
10+
for (var e : times) {
11+
g[e[0] - 1][e[1] - 1] = e[2];
1412
}
1513
dist[k - 1] = 0;
14+
boolean[] vis = new boolean[n];
1615
for (int i = 0; i < n; ++i) {
1716
int t = -1;
1817
for (int j = 0; j < n; ++j) {
@@ -26,9 +25,9 @@ public int networkDelayTime(int[][] times, int n, int k) {
2625
}
2726
}
2827
int ans = 0;
29-
for (int d : dist) {
30-
ans = Math.max(ans, d);
28+
for (int x : dist) {
29+
ans = Math.max(ans, x);
3130
}
32-
return ans == INF ? -1 : ans;
31+
return ans == inf ? -1 : ans;
3332
}
34-
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution:
22
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
3-
INF = 0x3F3F
4-
dist = [INF] * n
5-
vis = [False] * n
6-
g = [[INF] * n for _ in range(n)]
3+
g = [[inf] * n for _ in range(n)]
74
for u, v, w in times:
85
g[u - 1][v - 1] = w
6+
dist = [inf] * n
97
dist[k - 1] = 0
8+
vis = [False] * n
109
for _ in range(n):
1110
t = -1
1211
for j in range(n):
@@ -16,4 +15,4 @@ def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
1615
for j in range(n):
1716
dist[j] = min(dist[j], dist[t] + g[t][j])
1817
ans = max(dist)
19-
return -1 if ans == INF else ans
18+
return -1 if ans == inf else ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function networkDelayTime(times: number[][], n: number, k: number): number {
2+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(Infinity));
3+
for (const [u, v, w] of times) {
4+
g[u - 1][v - 1] = w;
5+
}
6+
const dist: number[] = Array(n).fill(Infinity);
7+
dist[k - 1] = 0;
8+
const vis: boolean[] = Array(n).fill(false);
9+
for (let i = 0; i < n; ++i) {
10+
let t = -1;
11+
for (let j = 0; j < n; ++j) {
12+
if (!vis[j] && (t === -1 || dist[j] < dist[t])) {
13+
t = j;
14+
}
15+
}
16+
vis[t] = true;
17+
for (let j = 0; j < n; ++j) {
18+
dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
19+
}
20+
}
21+
const ans = Math.max(...dist);
22+
return ans === Infinity ? -1 : ans;
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
class Solution {
22
public:
3-
const int inf = 0x3f3f;
4-
53
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
6-
vector<vector<vector<int>>> g(n);
7-
for (auto& t : times) g[t[0] - 1].push_back({t[1] - 1, t[2]});
4+
const int inf = 1 << 29;
5+
using pii = pair<int, int>;
6+
vector<vector<pii>> g(n);
7+
for (auto& edge : times) {
8+
g[edge[0] - 1].emplace_back(edge[1] - 1, edge[2]);
9+
}
10+
811
vector<int> dist(n, inf);
912
dist[k - 1] = 0;
10-
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
11-
q.push({0, k - 1});
12-
while (!q.empty()) {
13-
auto p = q.top();
14-
q.pop();
15-
int u = p[1];
16-
for (auto& ne : g[u]) {
17-
int v = ne[0], w = ne[1];
13+
priority_queue<pii, vector<pii>, greater<>> pq;
14+
pq.emplace(0, k - 1);
15+
16+
while (!pq.empty()) {
17+
auto [d, u] = pq.top();
18+
pq.pop();
19+
if (d > dist[u]) {
20+
continue;
21+
}
22+
for (auto& [v, w] : g[u]) {
1823
if (dist[v] > dist[u] + w) {
1924
dist[v] = dist[u] + w;
20-
q.push({dist[v], v});
25+
pq.emplace(dist[v], v);
2126
}
2227
}
2328
}
24-
int ans = *max_element(dist.begin(), dist.end());
29+
30+
int ans = ranges::max(dist);
2531
return ans == inf ? -1 : ans;
2632
}
27-
};
33+
};
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,42 @@
1-
const Inf = 0x3f3f3f3f
2-
3-
type pair struct {
4-
first int
5-
second int
6-
}
7-
8-
var _ heap.Interface = (*pairs)(nil)
9-
10-
type pairs []pair
11-
12-
func (a pairs) Len() int { return len(a) }
13-
func (a pairs) Less(i int, j int) bool {
14-
return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second
15-
}
16-
func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] }
17-
func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) }
18-
func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
19-
201
func networkDelayTime(times [][]int, n int, k int) int {
21-
graph := make([]pairs, n)
22-
for _, time := range times {
23-
from, to, time := time[0]-1, time[1]-1, time[2]
24-
graph[from] = append(graph[from], pair{to, time})
2+
g := make([][][2]int, n)
3+
for _, e := range times {
4+
u, v, w := e[0]-1, e[1]-1, e[2]
5+
g[u] = append(g[u], [2]int{v, w})
256
}
26-
27-
dis := make([]int, n)
28-
for i := range dis {
29-
dis[i] = Inf
7+
dist := make([]int, n)
8+
const inf int = 1 << 29
9+
for i := range dist {
10+
dist[i] = inf
3011
}
31-
dis[k-1] = 0
32-
33-
vis := make([]bool, n)
34-
h := make(pairs, 0)
35-
heap.Push(&h, pair{0, k - 1})
36-
for len(h) > 0 {
37-
from := heap.Pop(&h).(pair).second
38-
if vis[from] {
12+
dist[k-1] = 0
13+
pq := hp{{0, k - 1}}
14+
for len(pq) > 0 {
15+
p := heap.Pop(&pq).(pair)
16+
d, u := p.x, p.i
17+
if d > dist[u] {
3918
continue
4019
}
41-
vis[from] = true
42-
for _, e := range graph[from] {
43-
to, d := e.first, dis[from]+e.second
44-
if d < dis[to] {
45-
dis[to] = d
46-
heap.Push(&h, pair{d, to})
20+
for _, e := range g[u] {
21+
v, w := e[0], e[1]
22+
if nd := d + w; nd < dist[v] {
23+
dist[v] = nd
24+
heap.Push(&pq, pair{nd, v})
4725
}
4826
}
4927
}
50-
ans := slices.Max(dis)
51-
if ans == Inf {
52-
return -1
28+
if ans := slices.Max(dist); ans < inf {
29+
return ans
5330
}
54-
return ans
55-
}
31+
return -1
32+
33+
}
34+
35+
type pair struct{ x, i int }
36+
type hp []pair
37+
38+
func (h hp) Len() int { return len(h) }
39+
func (h hp) Less(i, j int) bool { return h[i].x < h[j].x }
40+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
41+
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
42+
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return }
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
class Solution {
2-
private static final int INF = 0x3f3f;
3-
42
public int networkDelayTime(int[][] times, int n, int k) {
3+
final int inf = 1 << 29;
54
List<int[]>[] g = new List[n];
6-
int[] dist = new int[n];
7-
for (int i = 0; i < n; ++i) {
8-
dist[i] = INF;
9-
g[i] = new ArrayList<>();
10-
}
11-
for (int[] t : times) {
12-
g[t[0] - 1].add(new int[] {t[1] - 1, t[2]});
5+
Arrays.setAll(g, i -> new ArrayList<>());
6+
for (var e : times) {
7+
g[e[0] - 1].add(new int[] {e[1] - 1, e[2]});
138
}
9+
int[] dist = new int[n];
10+
Arrays.fill(dist, inf);
1411
dist[k - 1] = 0;
15-
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
16-
q.offer(new int[] {0, k - 1});
17-
while (!q.isEmpty()) {
18-
int[] p = q.poll();
19-
int u = p[1];
20-
for (int[] ne : g[u]) {
21-
int v = ne[0], w = ne[1];
12+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
13+
pq.offer(new int[] {0, k - 1});
14+
while (!pq.isEmpty()) {
15+
var p = pq.poll();
16+
int d = p[0], u = p[1];
17+
if (d > dist[u]) {
18+
continue;
19+
}
20+
for (var e : g[u]) {
21+
int v = e[0], w = e[1];
2222
if (dist[v] > dist[u] + w) {
2323
dist[v] = dist[u] + w;
24-
q.offer(new int[] {dist[v], v});
24+
pq.offer(new int[] {dist[v], v});
2525
}
2626
}
2727
}
28-
int ans = 0;
29-
for (int d : dist) {
30-
ans = Math.max(ans, d);
31-
}
32-
return ans == INF ? -1 : ans;
28+
int ans = Arrays.stream(dist).max().getAsInt();
29+
return ans == inf ? -1 : ans;
3330
}
34-
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
class Solution:
22
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
3-
INF = 0x3F3F
4-
g = defaultdict(list)
3+
g = [[] for _ in range(n)]
54
for u, v, w in times:
65
g[u - 1].append((v - 1, w))
7-
dist = [INF] * n
6+
dist = [inf] * n
87
dist[k - 1] = 0
9-
q = [(0, k - 1)]
10-
while q:
11-
_, u = heappop(q)
8+
pq = [(0, k - 1)]
9+
while pq:
10+
d, u = heappop(pq)
11+
if d > dist[u]:
12+
continue
1213
for v, w in g[u]:
13-
if dist[v] > dist[u] + w:
14-
dist[v] = dist[u] + w
15-
heappush(q, (dist[v], v))
14+
if (nd := d + w) < dist[v]:
15+
dist[v] = nd
16+
heappush(pq, (nd, v))
1617
ans = max(dist)
17-
return -1 if ans == INF else ans
18+
return -1 if ans == inf else ans

0 commit comments

Comments
 (0)