743:网络延迟时间
This commit is contained in:
parent
cb5444c057
commit
d932a6878f
101
src/main/java/leetcode/editor/cn/NetworkDelayTime.java
Normal file
101
src/main/java/leetcode/editor/cn/NetworkDelayTime.java
Normal file
@ -0,0 +1,101 @@
|
||||
//有 n 个网络节点,标记为 1 到 n。
|
||||
//
|
||||
// 给你一个列表 times,表示信号经过 有向 边的传递时间。 times[i] = (ui, vi, wi),其中 ui 是源节点,vi 是目标节点, w
|
||||
//i 是一个信号从源节点传递到目标节点的时间。
|
||||
//
|
||||
// 现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//输入:times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:times = [[1,2,1]], n = 2, k = 1
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:times = [[1,2,1]], n = 2, k = 2
|
||||
//输出:-1
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= k <= n <= 100
|
||||
// 1 <= times.length <= 6000
|
||||
// times[i].length == 3
|
||||
// 1 <= ui, vi <= n
|
||||
// ui != vi
|
||||
// 0 <= wi <= 100
|
||||
// 所有 (ui, vi) 对都 互不相同(即,不含重复边)
|
||||
//
|
||||
// Related Topics 深度优先搜索 广度优先搜索 图 最短路 堆(优先队列)
|
||||
// 👍 312 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
//743:网络延迟时间
|
||||
public class NetworkDelayTime {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new NetworkDelayTime().new Solution();
|
||||
TwoArray twoArray = new TwoArray("[[2,1,1],[2,3,1],[3,4,1]]");
|
||||
System.out.println(solution.networkDelayTime(twoArray.getArr(), 4, 2));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int networkDelayTime(int[][] times, int n, int k) {
|
||||
final int INF = Integer.MAX_VALUE / 2;
|
||||
int[][] g = new int[n][n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Arrays.fill(g[i], INF);
|
||||
}
|
||||
for (int[] t : times) {
|
||||
int x = t[0] - 1, y = t[1] - 1;
|
||||
g[x][y] = t[2];
|
||||
}
|
||||
|
||||
int[] dist = new int[n];
|
||||
Arrays.fill(dist, INF);
|
||||
dist[k - 1] = 0;
|
||||
boolean[] used = new boolean[n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int x = -1;
|
||||
for (int y = 0; y < n; ++y) {
|
||||
if (!used[y] && (x == -1 || dist[y] < dist[x])) {
|
||||
x = y;
|
||||
}
|
||||
}
|
||||
used[x] = true;
|
||||
for (int y = 0; y < n; ++y) {
|
||||
dist[y] = Math.min(dist[y], dist[x] + g[x][y]);
|
||||
}
|
||||
}
|
||||
|
||||
int ans = Arrays.stream(dist).max().getAsInt();
|
||||
return ans == INF ? -1 : ans;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
45
src/main/java/leetcode/editor/cn/NetworkDelayTime.md
Normal file
45
src/main/java/leetcode/editor/cn/NetworkDelayTime.md
Normal file
@ -0,0 +1,45 @@
|
||||
<p>有 <code>n</code> 个网络节点,标记为 <code>1</code> 到 <code>n</code>。</p>
|
||||
|
||||
<p>给你一个列表 <code>times</code>,表示信号经过 <strong>有向</strong> 边的传递时间。 <code>times[i] = (u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>)</code>,其中 <code>u<sub>i</sub></code> 是源节点,<code>v<sub>i</sub></code> 是目标节点, <code>w<sub>i</sub></code> 是一个信号从源节点传递到目标节点的时间。</p>
|
||||
|
||||
<p>现在,从某个节点 <code>K</code> 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/05/23/931_example_1.png" style="height: 220px; width: 200px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>times = [[1,2,1]], n = 2, k = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>times = [[1,2,1]], n = 2, k = 2
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= n <= 100</code></li>
|
||||
<li><code>1 <= times.length <= 6000</code></li>
|
||||
<li><code>times[i].length == 3</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li><code>0 <= w<sub>i</sub> <= 100</code></li>
|
||||
<li>所有 <code>(u<sub>i</sub>, v<sub>i</sub>)</code> 对都 <strong>互不相同</strong>(即,不含重复边)</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>图</li><li>最短路</li><li>堆(优先队列)</li></div></div>\n<div><li>👍 312</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user