Skip to content

Commit 4483484

Browse files
committed
feat: add solutions to lc problem: No.0743
No.0743.Network Delay Time
1 parent cf2d2da commit 4483484

File tree

5 files changed

+234
-205
lines changed

5 files changed

+234
-205
lines changed

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

Lines changed: 81 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:朴素 Dijkstra 算法**
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
@@ -63,35 +65,26 @@
6365

6466
```python
6567
class Solution:
66-
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
67-
68-
# Build N+1 because index is from 1-N
69-
travel_times = [[] for _ in range(N + 1)]
70-
71-
# Build the array of travel times to reduce cost of searching later
72-
for time in times:
73-
origin, dest, time_travel = time
74-
travel_times[origin].append((dest, time_travel))
75-
76-
# Store the shortest amount of time to reach i-th node
77-
visited_times = [float('inf') for x in range(N + 1)]
78-
visited_times[0] = 0
79-
visited_times[K] = 0
80-
81-
# Store next traverse in line
82-
visited_queue = deque([K])
83-
84-
# BFS
85-
while visited_queue:
86-
cur_node = visited_queue.popleft()
87-
for time in travel_times[cur_node]:
88-
dest, time_travel = time
89-
if time_travel + visited_times[cur_node] < visited_times[dest]:
90-
visited_times[dest] = time_travel + visited_times[cur_node]
91-
visited_queue.append(dest)
92-
93-
# Only return the max if all were traversed. Return -1 otherwise
94-
return max(visited_times) if max(visited_times) != float('inf') else -1
68+
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
69+
N = 110
70+
INF = 0x3f3f
71+
g = [[INF] * N for _ in range(N)]
72+
for u, v, w in times:
73+
g[u][v] = w
74+
dist = [INF] * N
75+
dist[k] = 0
76+
vis = [False] * N
77+
for i in range(n):
78+
t = -1
79+
for j in range(1, n + 1):
80+
if not vis[j] and (t == -1 or dist[t] > dist[j]):
81+
t = j
82+
vis[t] = True
83+
for j in range(1, n + 1):
84+
dist[j] = min(dist[j], dist[t] + g[t][j])
85+
86+
ans = max(dist[1: n + 1])
87+
return -1 if ans == INF else ans
9588
```
9689

9790
### **Java**
@@ -100,61 +93,44 @@ class Solution:
10093

10194
```java
10295
class Solution {
103-
private static final int INF = 0x3f3f3f3f;
96+
private static final int N = 110;
97+
private static final int INF = 0x3f3f;
10498

10599
public int networkDelayTime(int[][] times, int n, int k) {
106-
List<List<Pair>> graph = new ArrayList<>();
107-
for (int i = 0; i < n; i++) {
108-
graph.add(new ArrayList<>());
109-
}
110-
for (int[] t : times) {
111-
int from = t[0] - 1, to = t[1] - 1, time = t[2];
112-
graph.get(from).add(new Pair(to, time));
100+
int[][] g = new int[N][N];
101+
for (int i = 0; i < N; ++i) {
102+
Arrays.fill(g[i], INF);
113103
}
114-
115-
List<Integer> dis = new ArrayList<>();
116-
for (int i = 0; i < n; i++) {
117-
dis.add(INF);
104+
for (int[] e : times) {
105+
g[e[0]][e[1]] = e[2];
118106
}
119-
dis.set(k - 1, 0);
120-
121-
Queue<Integer> queue = new ArrayDeque<>();
122-
queue.offer(k - 1);
123-
while (!queue.isEmpty()) {
124-
int from = queue.poll();
125-
for (Pair e : graph.get(from)) {
126-
int to = e.first, time = e.second;
127-
if (time + dis.get(from) < dis.get(to)) {
128-
dis.set(to, time + dis.get(from));
129-
queue.offer(to);
107+
int[] dist = new int[N];
108+
Arrays.fill(dist, INF);
109+
dist[k] = 0;
110+
boolean[] vis = new boolean[N];
111+
for (int i = 0; i < n; ++i) {
112+
int t = -1;
113+
for (int j = 1; j <= n; ++j) {
114+
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
115+
t = j;
130116
}
131117
}
118+
vis[t] = true;
119+
for (int j = 1; j <= n; ++j) {
120+
dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
121+
}
132122
}
133-
134-
int ans = Integer.MIN_VALUE;
135-
for (int d : dis) {
136-
ans = Math.max(ans, d);
123+
int ans = 0;
124+
for (int i = 1; i <= n; ++i) {
125+
ans = Math.max(ans, dist[i]);
137126
}
138-
139127
return ans == INF ? -1 : ans;
140128
}
141-
142-
static class Pair {
143-
private int first;
144-
private int second;
145-
146-
public Pair(int first, int second) {
147-
this.first = first;
148-
this.second = second;
149-
}
150-
}
151129
}
152130
```
153131

154132
### **Go**
155133

156-
Dijkstra
157-
158134
```go
159135
const Inf = 0x3f3f3f3f
160136

@@ -224,6 +200,42 @@ func max(x, y int) int {
224200
}
225201
```
226202

203+
### **C++**
204+
205+
```cpp
206+
class Solution {
207+
public:
208+
int N = 110;
209+
int INF = 0x3f3f;
210+
211+
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
212+
vector<vector<int>> g(N, vector<int>(N, INF));
213+
for (auto& e : times) g[e[0]][e[1]] = e[2];
214+
vector<int> dist(N, INF);
215+
dist[k] = 0;
216+
vector<bool> vis(N);
217+
for (int i = 0; i < n; ++i)
218+
{
219+
int t = -1;
220+
for (int j = 1; j <= n; ++j)
221+
{
222+
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
223+
{
224+
t = j;
225+
}
226+
}
227+
vis[t] = true;
228+
for (int j = 1; j <= n; ++j)
229+
{
230+
dist[j] = min(dist[j], dist[t] + g[t][j]);
231+
}
232+
}
233+
int ans = *max_element(dist.begin() + 1, dist.begin() + 1 + n);
234+
return ans == INF ? -1 : ans;
235+
}
236+
};
237+
```
238+
227239
### **...**
228240

229241
```

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

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -51,96 +51,70 @@
5151

5252
```python
5353
class Solution:
54-
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
55-
56-
# Build N+1 because index is from 1-N
57-
travel_times = [[] for _ in range(N + 1)]
58-
59-
# Build the array of travel times to reduce cost of searching later
60-
for time in times:
61-
origin, dest, time_travel = time
62-
travel_times[origin].append((dest, time_travel))
63-
64-
# Store the shortest amount of time to reach i-th node
65-
visited_times = [float('inf') for x in range(N + 1)]
66-
visited_times[0] = 0
67-
visited_times[K] = 0
68-
69-
# Store next traverse in line
70-
visited_queue = deque([K])
71-
72-
# BFS
73-
while visited_queue:
74-
cur_node = visited_queue.popleft()
75-
for time in travel_times[cur_node]:
76-
dest, time_travel = time
77-
if time_travel + visited_times[cur_node] < visited_times[dest]:
78-
visited_times[dest] = time_travel + visited_times[cur_node]
79-
visited_queue.append(dest)
80-
81-
# Only return the max if all were traversed. Return -1 otherwise
82-
return max(visited_times) if max(visited_times) != float('inf') else -1
54+
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
55+
N = 110
56+
INF = 0x3f3f
57+
g = [[INF] * N for _ in range(N)]
58+
for u, v, w in times:
59+
g[u][v] = w
60+
dist = [INF] * N
61+
dist[k] = 0
62+
vis = [False] * N
63+
for i in range(n):
64+
t = -1
65+
for j in range(1, n + 1):
66+
if not vis[j] and (t == -1 or dist[t] > dist[j]):
67+
t = j
68+
vis[t] = True
69+
for j in range(1, n + 1):
70+
dist[j] = min(dist[j], dist[t] + g[t][j])
71+
72+
ans = max(dist[1: n + 1])
73+
return -1 if ans == INF else ans
8374
```
8475

8576
### **Java**
8677

8778
```java
8879
class Solution {
89-
private static final int INF = 0x3f3f3f3f;
80+
private static final int N = 110;
81+
private static final int INF = 0x3f3f;
9082

9183
public int networkDelayTime(int[][] times, int n, int k) {
92-
List<List<Pair>> graph = new ArrayList<>();
93-
for (int i = 0; i < n; i++) {
94-
graph.add(new ArrayList<>());
84+
int[][] g = new int[N][N];
85+
for (int i = 0; i < N; ++i) {
86+
Arrays.fill(g[i], INF);
9587
}
96-
for (int[] t : times) {
97-
int from = t[0] - 1, to = t[1] - 1, time = t[2];
98-
graph.get(from).add(new Pair(to, time));
88+
for (int[] e : times) {
89+
g[e[0]][e[1]] = e[2];
9990
}
100-
101-
List<Integer> dis = new ArrayList<>();
102-
for (int i = 0; i < n; i++) {
103-
dis.add(INF);
104-
}
105-
dis.set(k - 1, 0);
106-
107-
Queue<Integer> queue = new ArrayDeque<>();
108-
queue.offer(k - 1);
109-
while (!queue.isEmpty()) {
110-
int from = queue.poll();
111-
for (Pair e : graph.get(from)) {
112-
int to = e.first, time = e.second;
113-
if (time + dis.get(from) < dis.get(to)) {
114-
dis.set(to, time + dis.get(from));
115-
queue.offer(to);
91+
int[] dist = new int[N];
92+
Arrays.fill(dist, INF);
93+
dist[k] = 0;
94+
boolean[] vis = new boolean[N];
95+
for (int i = 0; i < n; ++i) {
96+
int t = -1;
97+
for (int j = 1; j <= n; ++j) {
98+
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
99+
t = j;
116100
}
117101
}
102+
vis[t] = true;
103+
for (int j = 1; j <= n; ++j) {
104+
dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
105+
}
118106
}
119-
120-
int ans = Integer.MIN_VALUE;
121-
for (int d : dis) {
122-
ans = Math.max(ans, d);
107+
int ans = 0;
108+
for (int i = 1; i <= n; ++i) {
109+
ans = Math.max(ans, dist[i]);
123110
}
124-
125111
return ans == INF ? -1 : ans;
126112
}
127-
128-
static class Pair {
129-
private int first;
130-
private int second;
131-
132-
public Pair(int first, int second) {
133-
this.first = first;
134-
this.second = second;
135-
}
136-
}
137113
}
138114
```
139115

140116
### **Go**
141117

142-
Dijkstra 算法
143-
144118
```go
145119
const Inf = 0x3f3f3f3f
146120

@@ -210,6 +184,42 @@ func max(x, y int) int {
210184
}
211185
```
212186

187+
### **C++**
188+
189+
```cpp
190+
class Solution {
191+
public:
192+
int N = 110;
193+
int INF = 0x3f3f;
194+
195+
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
196+
vector<vector<int>> g(N, vector<int>(N, INF));
197+
for (auto& e : times) g[e[0]][e[1]] = e[2];
198+
vector<int> dist(N, INF);
199+
dist[k] = 0;
200+
vector<bool> vis(N);
201+
for (int i = 0; i < n; ++i)
202+
{
203+
int t = -1;
204+
for (int j = 1; j <= n; ++j)
205+
{
206+
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
207+
{
208+
t = j;
209+
}
210+
}
211+
vis[t] = true;
212+
for (int j = 1; j <= n; ++j)
213+
{
214+
dist[j] = min(dist[j], dist[t] + g[t][j]);
215+
}
216+
}
217+
int ans = *max_element(dist.begin() + 1, dist.begin() + 1 + n);
218+
return ans == INF ? -1 : ans;
219+
}
220+
};
221+
```
222+
213223
### **...**
214224

215225
```

0 commit comments

Comments
 (0)