分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

743. Network Delay Time

发布时间:2023-09-06 02:27责任编辑:赖小花关键词:暂无标签

There are N network nodes, labelled 1 to N.

Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target.

Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1.

Note:

  1. N will be in the range [1, 100].
  2. K will be in the range [1, N].
  3. The length of times will be in the range [1, 6000].
  4. All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 1 <= w <= 100.

Approach #1: C++.[Bellman-Ford]

class Solution {public: ???int networkDelayTime(vector<vector<int>>& times, int N, int K) { ???????const int MAX_TIME = 101 * 100; ???????vector<int> dist(N, MAX_TIME); ???????dist[K-1] = 0; ???????for (int i = 0; i < N; ++i) { ???????????for (auto time : times) { ???????????????int u = time[0]-1, v = time[1] - 1, w = time[2]; ???????????????dist[v] = min(dist[v], dist[u] + w); ???????????} ???????} ???????int temp = *max_element(dist.begin(), dist.end()); ???????return temp == MAX_TIME ? -1 : temp; ???}};

  

Approach #2: C++. [Floyd-Warshall]

// Author: Huahua// Runtime: 89 msclass Solution {public: ???int networkDelayTime(vector<vector<int>>& times, int N, int K) { ???????constexpr int MAX_TIME = 101 * 100; ???????vector<vector<int>> d(N, vector<int>(N, MAX_TIME)); ???????????????for (auto time : times) ??????????????d[time[0] - 1][time[1] - 1] = time[2]; ???????????????for (int i = 0; i < N; ++i) ???????????d[i][i] = 0; ???????????????for (int k = 0; k < N; ++k) ???????????for (int i = 0; i < N; ++i) ???????????????for (int j = 0; j < N; ++j) ???????????????????????????????????????d[i][j] = min(d[i][j], d[i][k] + d[k][j]); ???????????????int max_time = *max_element(d[K - 1].begin(), d[K - 1].end()); ???????????????return max_time >= MAX_TIME ? - 1 : max_time; ???}};

  

Appraoch #3: Java. [Dijkstra‘s Algorithm]

class Solution { ???Map<Integer, Integer> dist; ???public int networkDelayTime(int[][] times, int N, int K) { ???????Map<Integer, List<int[]>> graph = new HashMap(); ???????for (int[] edge : times) { ???????????if (!graph.containsKey(edge[0])) { ???????????????graph.put(edge[0], new ArrayList<int[]>()); ???????????} ???????????graph.get(edge[0]).add(new int[]{edge[1], edge[2]}); ???????} ???????dist = new HashMap(); ???????for (int node = 1; node <= N; ++node) { ???????????dist.put(node, Integer.MAX_VALUE); ???????} ???????dist.put(K, 0); ???????boolean[] seen = new boolean[N+1]; ???????while (true) { ???????????int candNode = -1; ???????????int candDist = Integer.MAX_VALUE; ???????????for (int i = 1; i <= N; ++i) { ???????????????if (!seen[i] && dist.get(i) < candDist) { ???????????????????candDist = dist.get(i); ???????????????????candNode = i; ???????????????} ???????????} ???????????if (candNode < 0) break; ???????????seen[candNode] = true; ???????????if (graph.containsKey(candNode)) { ???????????????for (int[] info : graph.get(candNode)) ???????????????????dist.put(info[0], Math.min(dist.get(info[0]), dist.get(candNode) + info[1])); ???????????} ???????} ???????int ans = 0; ???????for (int cand : dist.values()) { ???????????if (cand == Integer.MAX_VALUE) return -1; ???????????ans = Math.max(ans, cand); ???????} ???????return ans; ???}}

  

Appraoch #4: Python[DFS]

class Solution(object): ???def networkDelayTime(self, times, N, K): ???????""" ???????:type times: List[List[int]] ???????:type N: int ???????:type K: int ???????:rtype: int ???????""" ???????graph = collections.defaultdict(list) ???????????????for u, v, w in times: ???????????graph[u].append((w, v)) ???????????????????dist = {node: float(‘inf‘) for node in xrange(1, N+1)} ???????????????def dfs(node, elapsed): ???????????if elapsed >= dist[node]: return ???????????dist[node] = elapsed ???????????for time, nei in sorted(graph[node]): ???????????????dfs(nei, elapsed + time) ???????????????????dfs(K, 0) ???????????????ans = max(dist.values()) ???????return ans if ans < float(‘inf‘) else -1 ???

  

743. Network Delay Time

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10160581.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved