Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit82fcb95

Browse files
committed
743-network-delay-time.md AddedDijkstra's algorithm solutions.
1 parent54b7c35 commit82fcb95

File tree

3 files changed

+357
-15
lines changed

3 files changed

+357
-15
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,6 @@ You can skip the more difficult problems and do them later.
115115
-[1584. Min Cost to Connect All Points](en/1001-2000/1584-min-cost-to-connect-all-points.md) was solved in_Python, Java, C++, JavaScript, C#, Go, Ruby_ and 2 ways.
116116
-[207. Course Schedule](en/1-1000/207-course-schedule.md) was solved in_Python, Java, C++, C#_ and 2 ways.
117117
-[1514. Path with Maximum Probability](en/1001-2000/1514-path-with-maximum-probability.md) was solved in_Python_ and 2 ways.
118+
-[743. Network Delay Time](en/1-1000/743-network-delay-time.md) was solved in_Python_ and 2 ways.
118119

119-
More LeetCode problems will be added soon...
120+
More LeetCode problems will be added soon.

‎en/1-1000/743-network-delay-time.md

Lines changed: 128 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,149 @@ We will send a signal from a given node `k`. Return _the **minimum** time it tak
3939
</details>
4040

4141
##Intuition
42+
We can solve it via both**Bellman-Ford algorithm** and**Dijkstra's algorithm**.
43+
44+
`Bellman-Ford algorithm` has less code and can handle the situation of**negative** edge weights, but it is slower than`Dijkstra's algorithm`.
45+
46+
`Dijkstra's algorithm` always takes the shortest path, so it is more efficient, but it requires writing more code and it cannot handle the situation of**negative** edge weights.
47+
48+
For a detailed description of**Dijkstra's algorithm**, please refer to[1514. Path with Maximum Probability](../1001-2000/1514-path-with-maximum-probability.md).
4249

4350
##Complexity
44-
* Time:`O()`.
45-
* Space:`O()`.
51+
**V**: vertex count,**E**: Edge count.
52+
53+
###Bellman-Ford algorithm
54+
* Time:`O(V * E)`.
55+
* Space:`O(V)`.
56+
57+
###Dijkstra's algorithm using`heap sort`
58+
* Time:`O(E * log(E))`.
59+
* Space:`O(V + E)`.
4660

4761
##Python
62+
###Standard Bellman-Ford algorithm
4863
```python
4964
classSolution:
50-
defnetworkDelayTime(self,items: List[List[int]],n:int,start:int) ->int:
51-
min_distances= [float('inf')]* (n+1)
52-
min_distances[start]=0
65+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start:int) ->int:
66+
min_times= [float('inf')]* (n+1)
67+
min_times[start]=0
68+
69+
for _inrange(n-1):
70+
for source_node, target_node, timein edges:# process edges directly
71+
if min_times[source_node]==float('inf'):
72+
continue
5373

74+
target_time= time+ min_times[source_node]
75+
76+
if target_time< min_times[target_node]:
77+
min_times[target_node]= target_time
78+
79+
result=max(min_times[1:])
80+
81+
if result==float('inf'):
82+
return-1
83+
84+
return result
85+
```
86+
87+
###Algorithm similar to Bellman-Ford algorithm
88+
```python
89+
classSolution:
90+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start:int) ->int:
91+
min_times= [float('inf')]* (n+1)
92+
min_times[start]=0
5493
node_to_pairs= defaultdict(set)
5594

56-
for source, target,distanceinitems:
57-
node_to_pairs[source].add((target,distance))
95+
for source, target,timeinedges:# process nodes first, then their edges
96+
node_to_pairs[source].add((target,time))
5897

5998
for _inrange(n-1):
60-
for node, min_distanceinenumerate(min_distances):
61-
if min_distance==float('inf'):
99+
for node, min_timeinenumerate(min_times):# process nodes first
100+
if min_time==float('inf'):
101+
continue
102+
103+
for target_node, timein node_to_pairs[node]:# process edges of the node
104+
target_time= time+ min_time
105+
106+
if target_time< min_times[target_node]:
107+
min_times[target_node]= target_time
108+
109+
result=max(min_times[1:])
110+
111+
if result==float('inf'):
112+
return-1
113+
114+
return result
115+
```
116+
117+
###Dijkstra's algorithm using`heap sort` (without`visited`)
118+
```python
119+
import heapq
120+
121+
classSolution:
122+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start_node:int) ->int:
123+
min_times= [float('inf')]* (n+1)
124+
min_times[start_node]=0
125+
node_to_pairs= defaultdict(set)
126+
127+
for source, target, timein edges:
128+
node_to_pairs[source].add((target, time))
129+
130+
priority_queue= [(0, start_node)]
131+
132+
while priority_queue:
133+
current_time, current_node= heapq.heappop(priority_queue)
134+
135+
for target_node, timein node_to_pairs[current_node]:
136+
target_time= time+ current_time
137+
138+
if target_time< min_times[target_node]:
139+
min_times[target_node]= target_time
140+
heapq.heappush(priority_queue, (target_time, target_node))
141+
142+
result=max(min_times[1:])
143+
144+
if result==float('inf'):
145+
return-1
146+
147+
return result
148+
```
149+
150+
###Dijkstra's algorithm using`heap sort` (with`visited`)
151+
```python
152+
import heapq
153+
154+
classSolution:
155+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start_node:int) ->int:
156+
min_times= [float('inf')]* (n+1)
157+
min_times[start_node]=0
158+
node_to_pairs= defaultdict(set)
159+
visited= [False]* (n+1)# added 1
160+
161+
for source, target, timein edges:
162+
node_to_pairs[source].add((target, time))
163+
164+
priority_queue= [(0, start_node)]
165+
166+
while priority_queue:
167+
current_time, current_node= heapq.heappop(priority_queue)
168+
169+
if visited[current_node]:# added 3
170+
continue
171+
172+
visited[current_node]=True# added 2
173+
174+
for target_node, timein node_to_pairs[current_node]:
175+
if visited[target_node]:# added 4
62176
continue
63177

64-
for target_node, distancein node_to_pairs[node]:
65-
distance_= distance+ min_distance
178+
target_time= time+ current_time
66179

67-
if distance_< min_distances[target_node]:
68-
min_distances[target_node]= distance_
180+
if target_time< min_times[target_node]:
181+
min_times[target_node]= target_time
182+
heapq.heappush(priority_queue, (target_time, target_node))
69183

70-
result=max(min_distances[1:])
184+
result=max(min_times[1:])
71185

72186
if result==float('inf'):
73187
return-1

‎zh/1-1000/743-network-delay-time.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#743. 网络延迟时间 - 力扣题解最佳实践
2+
力扣链接:[743. 网络延迟时间](https://leetcode.cn/problems/network-delay-time), 难度:**中等**
3+
4+
##力扣“743. 网络延迟时间”问题描述
5+
`n` 个网络节点,标记为`1``n`
6+
7+
给你一个列表`times`,表示信号经过**有向** 边的传递时间。`times[i] = (ui, vi, wi)`,其中`ui` 是源节点,`vi` 是目标节点,`wi` 是一个信号从源节点传递到目标节点的时间。
8+
9+
现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回`-1`
10+
11+
###[示例 1]
12+
![](../../images/examples/743_1.png)
13+
14+
**输入**:`times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2`
15+
16+
**输出**:`2`
17+
18+
###[示例 2]
19+
**输入**:`times = [[1,2,1]], n = 2, k = 1`
20+
21+
**输出**:`1`
22+
23+
###[示例 3]
24+
**输入**:`times = [[1,2,1]], n = 2, k = 2`
25+
26+
**输出**:`-1`
27+
28+
###[约束]
29+
-`1 <= k <= n <= 100`
30+
-`1 <= times.length <= 6000`
31+
-`times[i].length == 3`
32+
-`1 <= ui, vi <= n`
33+
-`ui != vi`
34+
-`0 <= wi <= 100`
35+
- 所有`(ui, vi)` 对都**互不相同**(即,不含重复边)
36+
37+
###[提示]
38+
<details>
39+
<summary>提示 1</summary>
40+
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
41+
</details>
42+
43+
##思路
44+
本题可用**Bellman-Ford算法****Dijkstra算法** 解决。
45+
46+
`Bellman-Ford`算法代码量少,还能处理``权值为**负数**的情况,但较慢。
47+
48+
`Dijkstra算法`因为始终走最短路径,所以执行**效率高**!但代码量大,无法处理``权值为**负数**的情况。
49+
50+
**Dijkstra算法**的详细说明,请参考[1514. 概率最大的路径](../1001-2000/1514-path-with-maximum-probability.md)
51+
52+
##复杂度
53+
**V**: 顶点数量,**E**: 边的数量。
54+
55+
###Bellman-Ford 算法
56+
* 时间:`O(V * E)`.
57+
* 空间:`O(V)`.
58+
59+
###Dijkstra 算法(采用`堆排序`
60+
* 时间:`O(E * log(E))`.
61+
* 空间:`O(V + E)`.
62+
63+
##Python
64+
###Standard Bellman-Ford algorithm
65+
```python
66+
classSolution:
67+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start:int) ->int:
68+
min_times= [float('inf')]* (n+1)
69+
min_times[start]=0
70+
71+
for _inrange(n-1):
72+
for source_node, target_node, timein edges:# process edges directly
73+
if min_times[source_node]==float('inf'):
74+
continue
75+
76+
target_time= time+ min_times[source_node]
77+
78+
if target_time< min_times[target_node]:
79+
min_times[target_node]= target_time
80+
81+
result=max(min_times[1:])
82+
83+
if result==float('inf'):
84+
return-1
85+
86+
return result
87+
```
88+
89+
###Algorithm similar to Bellman-Ford algorithm
90+
```python
91+
classSolution:
92+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start:int) ->int:
93+
min_times= [float('inf')]* (n+1)
94+
min_times[start]=0
95+
node_to_pairs= defaultdict(set)
96+
97+
for source, target, timein edges:# process nodes first, then their edges
98+
node_to_pairs[source].add((target, time))
99+
100+
for _inrange(n-1):
101+
for node, min_timeinenumerate(min_times):# process nodes first
102+
if min_time==float('inf'):
103+
continue
104+
105+
for target_node, timein node_to_pairs[node]:# process edges of the node
106+
target_time= time+ min_time
107+
108+
if target_time< min_times[target_node]:
109+
min_times[target_node]= target_time
110+
111+
result=max(min_times[1:])
112+
113+
if result==float('inf'):
114+
return-1
115+
116+
return result
117+
```
118+
119+
###Dijkstra's algorithm using`heap sort` (without`visited`)
120+
```python
121+
import heapq
122+
123+
classSolution:
124+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start_node:int) ->int:
125+
min_times= [float('inf')]* (n+1)
126+
min_times[start_node]=0
127+
node_to_pairs= defaultdict(set)
128+
129+
for source, target, timein edges:
130+
node_to_pairs[source].add((target, time))
131+
132+
priority_queue= [(0, start_node)]
133+
134+
while priority_queue:
135+
current_time, current_node= heapq.heappop(priority_queue)
136+
137+
for target_node, timein node_to_pairs[current_node]:
138+
target_time= time+ current_time
139+
140+
if target_time< min_times[target_node]:
141+
min_times[target_node]= target_time
142+
heapq.heappush(priority_queue, (target_time, target_node))
143+
144+
result=max(min_times[1:])
145+
146+
if result==float('inf'):
147+
return-1
148+
149+
return result
150+
```
151+
152+
###Dijkstra's algorithm using`heap sort` (with`visited`)
153+
```python
154+
import heapq
155+
156+
classSolution:
157+
defnetworkDelayTime(self,edges: List[List[int]],n:int,start_node:int) ->int:
158+
min_times= [float('inf')]* (n+1)
159+
min_times[start_node]=0
160+
node_to_pairs= defaultdict(set)
161+
visited= [False]* (n+1)# added 1
162+
163+
for source, target, timein edges:
164+
node_to_pairs[source].add((target, time))
165+
166+
priority_queue= [(0, start_node)]
167+
168+
while priority_queue:
169+
current_time, current_node= heapq.heappop(priority_queue)
170+
171+
if visited[current_node]:# added 3
172+
continue
173+
174+
visited[current_node]=True# added 2
175+
176+
for target_node, timein node_to_pairs[current_node]:
177+
if visited[target_node]:# added 4
178+
continue
179+
180+
target_time= time+ current_time
181+
182+
if target_time< min_times[target_node]:
183+
min_times[target_node]= target_time
184+
heapq.heappush(priority_queue, (target_time, target_node))
185+
186+
result=max(min_times[1:])
187+
188+
if result==float('inf'):
189+
return-1
190+
191+
return result
192+
```
193+
194+
##JavaScript
195+
```javascript
196+
// Welcome to create a PR to complete the code of this language, thanks!
197+
```
198+
199+
##Java
200+
```java
201+
// Welcome to create a PR to complete the code of this language, thanks!
202+
```
203+
204+
##C++
205+
```cpp
206+
// Welcome to create a PR to complete the code of this language, thanks!
207+
```
208+
209+
##C#
210+
```c#
211+
// Welcome to create a PR to complete the code of this language, thanks!
212+
```
213+
214+
##Go
215+
```go
216+
// Welcome to create a PR to complete the code of this language, thanks!
217+
```
218+
219+
##Ruby
220+
```ruby
221+
# Welcome to create a PR to complete the code of this language, thanks!
222+
```
223+
224+
##C, Kotlin, Swift, Rust or other languages
225+
```
226+
// Welcome to create a PR to complete the code of this language, thanks!
227+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp