Skip to content

Latest commit

 

History

History
237 lines (180 loc) · 6.14 KB

File metadata and controls

237 lines (180 loc) · 6.14 KB

English Version

题目描述

n 个网络节点,标记为 1 到 n

给你一个列表 times,表示信号经过 有向 边的传递时间。 times[i] = (ui, vi, wi),其中 ui 是源节点,vi 是目标节点, wi 是一个信号从源节点传递到目标节点的时间。

现在,从某个节点 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) 对都 互不相同(即,不含重复边)

解法

Python3

from collections import deque
class Solution:
    def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:

        # Build N+1 because index is from 1-N
        travel_times = [[] for y in range(N+1)]

        # Build the array of travel times to reduce cost of searching later
        for time in times:
            origin, dest, time_travel = time
            travel_times[origin].append((dest, time_travel))

        # Store the shortest amount of time to reach i-th node
        visited_times = [float('inf') for x in range(N+1)]
        visited_times[0] = 0
        visited_times[K] = 0


        # Store next traverse in line
        visited_queue = deque()
        visited_queue.append(K)

        # BFS
        while visited_queue:
            cur_node = visited_queue.popleft()
            for time in travel_times[cur_node]:
                (dest, time_travel) = time
                if time_travel + visited_times[cur_node] < visited_times[dest]:
                    visited_times[dest] = time_travel + visited_times[cur_node]
                    visited_queue.append(dest)

        # Only return the max if all were traversed. Return -1 otherwise
        return max(visited_times) if max(visited_times) != float('inf') else -1

Java

class Solution {
    private static final int INF = 0x3f3f3f3f;

    public int networkDelayTime(int[][] times, int n, int k) {
        List<List<Pair>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int[] t : times) {
            int from = t[0] - 1, to = t[1] - 1, time = t[2];
            graph.get(from).add(new Pair(to, time));
        }

        List<Integer> dis = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            dis.add(INF);
        }
        dis.set(k - 1, 0);

        Queue<Integer> queue = new ArrayDeque<>();
        queue.offer(k - 1);
        while (!queue.isEmpty()) {
            int from = queue.poll();
            for (Pair e : graph.get(from)) {
                int to = e.first, time = e.second;
                if (time + dis.get(from) < dis.get(to)) {
                    dis.set(to, time + dis.get(from));
                    queue.offer(to);
                }
            }
        }

        int ans = Integer.MIN_VALUE;
        for (int d : dis) {
            ans = Math.max(ans, d);
        }

        return ans == INF ? -1 : ans;
    }

    static class Pair {
        private int first;
        private int second;

        public Pair(int first, int second) {
            this.first = first;
            this.second = second;
        }
    }
}

Go

Dijkstra

const Inf = 0x3f3f3f3f

type pair struct {
	first  int
	second int
}

var _ heap.Interface = (*pairs)(nil)

type pairs []pair

func (a pairs) Len() int { return len(a) }
func (a pairs) Less(i int, j int) bool {
	return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second
}
func (a pairs) Swap(i int, j int)   { a[i], a[j] = a[j], a[i] }
func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) }
func (a *pairs) Pop() interface{}   { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }

func networkDelayTime(times [][]int, n int, k int) int {
	graph := make([]pairs, n)
	for _, time := range times {
		from, to, time := time[0]-1, time[1]-1, time[2]
		graph[from] = append(graph[from], pair{to, time})
	}

	dis := make([]int, n)
	for i := range dis {
		dis[i] = Inf
	}
	dis[k-1] = 0

	vis := make([]bool, n)
	h := make(pairs, 0)
	heap.Push(&h, pair{0, k - 1})
	for len(h) > 0 {
		from := heap.Pop(&h).(pair).second
		if vis[from] {
			continue
		}
		vis[from] = true
		for _, e := range graph[from] {
			to, d := e.first, dis[from]+e.second
			if d < dis[to] {
				dis[to] = d
				heap.Push(&h, pair{d, to})
			}
		}
	}

	ans := math.MinInt32
	for _, d := range dis {
		ans = max(ans, d)
	}
	if ans == Inf {
		return -1
	}
	return ans
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

...