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

Commit e3c68a3

Browse files
feat: add solutions to lc problem: No.0743.Network Delay Time
1 parent 65a87b7 commit e3c68a3

File tree

5 files changed

+433
-9
lines changed

5 files changed

+433
-9
lines changed

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

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,169 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
from collections import deque
67+
class Solution:
68+
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
69+
70+
# Build N+1 because index is from 1-N
71+
travel_times = [[] for y in range(N+1)]
72+
73+
# Build the array of travel times to reduce cost of searching later
74+
for time in times:
75+
origin, dest, time_travel = time
76+
travel_times[origin].append((dest, time_travel))
77+
78+
# Store the shortest amount of time to reach i-th node
79+
visited_times = [float('inf') for x in range(N+1)]
80+
visited_times[0] = 0
81+
visited_times[K] = 0
82+
83+
84+
# Store next traverse in line
85+
visited_queue = deque()
86+
visited_queue.append(K)
87+
88+
# BFS
89+
while visited_queue:
90+
cur_node = visited_queue.popleft()
91+
for time in travel_times[cur_node]:
92+
(dest, time_travel) = time
93+
if time_travel + visited_times[cur_node] < visited_times[dest]:
94+
visited_times[dest] = time_travel + visited_times[cur_node]
95+
visited_queue.append(dest)
96+
97+
# Only return the max if all were traversed. Return -1 otherwise
98+
return max(visited_times) if max(visited_times) != float('inf') else -1
6799
```
68100

69101
### **Java**
70102

71103
<!-- 这里可写当前语言的特殊实现逻辑 -->
72104

73105
```java
106+
class Solution {
107+
private static final int INF = 0x3f3f3f3f;
108+
109+
public int networkDelayTime(int[][] times, int n, int k) {
110+
List<List<Pair>> graph = new ArrayList<>();
111+
for (int i = 0; i < n; i++) {
112+
graph.add(new ArrayList<>());
113+
}
114+
for (int[] t : times) {
115+
int from = t[0] - 1, to = t[1] - 1, time = t[2];
116+
graph.get(from).add(new Pair(to, time));
117+
}
118+
119+
List<Integer> dis = new ArrayList<>();
120+
for (int i = 0; i < n; i++) {
121+
dis.add(INF);
122+
}
123+
dis.set(k - 1, 0);
124+
125+
Queue<Integer> queue = new ArrayDeque<>();
126+
queue.offer(k - 1);
127+
while (!queue.isEmpty()) {
128+
int from = queue.poll();
129+
for (Pair e : graph.get(from)) {
130+
int to = e.first, time = e.second;
131+
if (time + dis.get(from) < dis.get(to)) {
132+
dis.set(to, time + dis.get(from));
133+
queue.offer(to);
134+
}
135+
}
136+
}
137+
138+
int ans = Integer.MIN_VALUE;
139+
for (int d : dis) {
140+
ans = Math.max(ans, d);
141+
}
142+
143+
return ans == INF ? -1 : ans;
144+
}
145+
146+
static class Pair {
147+
private int first;
148+
private int second;
149+
150+
public Pair(int first, int second) {
151+
this.first = first;
152+
this.second = second;
153+
}
154+
}
155+
}
156+
```
74157

158+
### **Go**
159+
160+
Dijkstra
161+
162+
```go
163+
const Inf = 0x3f3f3f3f
164+
165+
type pair struct {
166+
first int
167+
second int
168+
}
169+
170+
var _ heap.Interface = (*pairs)(nil)
171+
172+
type pairs []pair
173+
174+
func (a pairs) Len() int { return len(a) }
175+
func (a pairs) Less(i int, j int) bool {
176+
return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second
177+
}
178+
func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] }
179+
func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) }
180+
func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
181+
182+
func networkDelayTime(times [][]int, n int, k int) int {
183+
graph := make([]pairs, n)
184+
for _, time := range times {
185+
from, to, time := time[0]-1, time[1]-1, time[2]
186+
graph[from] = append(graph[from], pair{to, time})
187+
}
188+
189+
dis := make([]int, n)
190+
for i := range dis {
191+
dis[i] = Inf
192+
}
193+
dis[k-1] = 0
194+
195+
vis := make([]bool, n)
196+
h := make(pairs, 0)
197+
heap.Push(&h, pair{0, k - 1})
198+
for len(h) > 0 {
199+
from := heap.Pop(&h).(pair).second
200+
if vis[from] {
201+
continue
202+
}
203+
vis[from] = true
204+
for _, e := range graph[from] {
205+
to, d := e.first, dis[from]+e.second
206+
if d < dis[to] {
207+
dis[to] = d
208+
heap.Push(&h, pair{d, to})
209+
}
210+
}
211+
}
212+
213+
ans := math.MinInt32
214+
for _, d := range dis {
215+
ans = max(ans, d)
216+
}
217+
if ans == Inf {
218+
return -1
219+
}
220+
return ans
221+
}
222+
223+
func max(x, y int) int {
224+
if x > y {
225+
return x
226+
}
227+
return y
228+
}
75229
```
76230

77231
### **...**

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

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,167 @@
5151
### **Python3**
5252

5353
```python
54-
54+
from collections import deque
55+
class Solution:
56+
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
57+
58+
# Build N+1 because index is from 1-N
59+
travel_times = [[] for y in range(N+1)]
60+
61+
# Build the array of travel times to reduce cost of searching later
62+
for time in times:
63+
origin, dest, time_travel = time
64+
travel_times[origin].append((dest, time_travel))
65+
66+
# Store the shortest amount of time to reach i-th node
67+
visited_times = [float('inf') for x in range(N+1)]
68+
visited_times[0] = 0
69+
visited_times[K] = 0
70+
71+
72+
# Store next traverse in line
73+
visited_queue = deque()
74+
visited_queue.append(K)
75+
76+
# BFS
77+
while visited_queue:
78+
cur_node = visited_queue.popleft()
79+
for time in travel_times[cur_node]:
80+
(dest, time_travel) = time
81+
if time_travel + visited_times[cur_node] < visited_times[dest]:
82+
visited_times[dest] = time_travel + visited_times[cur_node]
83+
visited_queue.append(dest)
84+
85+
# Only return the max if all were traversed. Return -1 otherwise
86+
return max(visited_times) if max(visited_times) != float('inf') else -1
5587
```
5688

5789
### **Java**
5890

5991
```java
92+
class Solution {
93+
private static final int INF = 0x3f3f3f3f;
94+
95+
public int networkDelayTime(int[][] times, int n, int k) {
96+
List<List<Pair>> graph = new ArrayList<>();
97+
for (int i = 0; i < n; i++) {
98+
graph.add(new ArrayList<>());
99+
}
100+
for (int[] t : times) {
101+
int from = t[0] - 1, to = t[1] - 1, time = t[2];
102+
graph.get(from).add(new Pair(to, time));
103+
}
104+
105+
List<Integer> dis = new ArrayList<>();
106+
for (int i = 0; i < n; i++) {
107+
dis.add(INF);
108+
}
109+
dis.set(k - 1, 0);
110+
111+
Queue<Integer> queue = new ArrayDeque<>();
112+
queue.offer(k - 1);
113+
while (!queue.isEmpty()) {
114+
int from = queue.poll();
115+
for (Pair e : graph.get(from)) {
116+
int to = e.first, time = e.second;
117+
if (time + dis.get(from) < dis.get(to)) {
118+
dis.set(to, time + dis.get(from));
119+
queue.offer(to);
120+
}
121+
}
122+
}
123+
124+
int ans = Integer.MIN_VALUE;
125+
for (int d : dis) {
126+
ans = Math.max(ans, d);
127+
}
128+
129+
return ans == INF ? -1 : ans;
130+
}
131+
132+
static class Pair {
133+
private int first;
134+
private int second;
135+
136+
public Pair(int first, int second) {
137+
this.first = first;
138+
this.second = second;
139+
}
140+
}
141+
}
142+
```
60143

144+
### **Go**
145+
146+
Dijkstra 算法
147+
148+
```go
149+
const Inf = 0x3f3f3f3f
150+
151+
type pair struct {
152+
first int
153+
second int
154+
}
155+
156+
var _ heap.Interface = (*pairs)(nil)
157+
158+
type pairs []pair
159+
160+
func (a pairs) Len() int { return len(a) }
161+
func (a pairs) Less(i int, j int) bool {
162+
return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second
163+
}
164+
func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] }
165+
func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) }
166+
func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
167+
168+
func networkDelayTime(times [][]int, n int, k int) int {
169+
graph := make([]pairs, n)
170+
for _, time := range times {
171+
from, to, time := time[0]-1, time[1]-1, time[2]
172+
graph[from] = append(graph[from], pair{to, time})
173+
}
174+
175+
dis := make([]int, n)
176+
for i := range dis {
177+
dis[i] = Inf
178+
}
179+
dis[k-1] = 0
180+
181+
vis := make([]bool, n)
182+
h := make(pairs, 0)
183+
heap.Push(&h, pair{0, k - 1})
184+
for len(h) > 0 {
185+
from := heap.Pop(&h).(pair).second
186+
if vis[from] {
187+
continue
188+
}
189+
vis[from] = true
190+
for _, e := range graph[from] {
191+
to, d := e.first, dis[from]+e.second
192+
if d < dis[to] {
193+
dis[to] = d
194+
heap.Push(&h, pair{d, to})
195+
}
196+
}
197+
}
198+
199+
ans := math.MinInt32
200+
for _, d := range dis {
201+
ans = max(ans, d)
202+
}
203+
if ans == Inf {
204+
return -1
205+
}
206+
return ans
207+
}
208+
209+
func max(x, y int) int {
210+
if x > y {
211+
return x
212+
}
213+
return y
214+
}
61215
```
62216

63217
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /