forked from xhofe/daily-problem-of-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-the-time-when-the-network-becomes-idle.rs
39 lines (38 loc) · 1.38 KB
/
20-the-time-when-the-network-becomes-idle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// https://leetcode-cn.com/problems/the-time-when-the-network-becomes-idle/
use std::collections::VecDeque;
impl Solution {
pub fn network_becomes_idle(edges: Vec<Vec<i32>>, patience: Vec<i32>) -> i32 {
let num_point = patience.len();
let mut graph = vec![vec![]; num_point];
edges.iter().for_each(|x| {
graph[x[0] as usize].push(x[1] as usize);
graph[x[1] as usize].push(x[0] as usize);
});
let mut distance = vec![num_point as i32; num_point];
let mut visited = vec![false; num_point];
visited[0] = true;
distance[0] = 0;
let mut queue = VecDeque::new();
queue.push_back(0);
while !queue.is_empty() {
let cur = queue.pop_front().unwrap();
graph[cur].iter().for_each(|&x| {
if !visited[x] {
distance[x] = distance[cur] + 1;
visited[x] = true;
queue.push_back(x);
}
})
}
let mut ans = 0;
for i in 1..num_point {
ans = ans.max(2 * distance[i] + 1 + if distance[i] * 2 <= patience[i] {
0
} else {
// 计算最后一次消息是什么时候发的
(2 * distance[i] - 1) / patience[i] * patience[i]
})
}
ans
}
}