LeetCode743 Network Delay Time
we are given a directed weighted graph, and we know that the node from 1 to N in this graph. now we are given the starting node k, now we need to know the farthest node from starting node k. return the umber of distance.
example:
Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
Output: 2
so as most problems related to graph, thid problem can be solved in dfs and bfs.
but think about this: does that this problem is a longest path problem? so it’s like a midified dijkstra. instead of searching for the shortest path, we search for the longest path starting from a fixed point.
first, let’s take a look at the dfs way
class Solution {
Map<Integer, Integer> dist; //it is a hashmap that store the <node, distance_from_k_to_current_node>
public int networkDelayTime(int[][] times, int N, int K) {
Map<Integer, List<int[]>> graph = new HashMap(); //graph uses nodes as key, and it's neighbor and weight cmbined as the value
for (int[] edge: times) {
if (!graph.containsKey(edge[0]))
graph.put(edge[0], new ArrayList<int[]>());
graph.get(edge[0]).add(new int[]{edge[2], edge[1]});
}
for (int node: graph.keySet()) {
Collections.sort(graph.get(node), (a, b) -> a[0] - b[0]); //sort each node's neighbor based on its weight, so that each time we choose neighborhood, we choose the shortest one, this is really important. but if we mocve this sort, the code can stil lbe accepted, just much slower.
}
dist = new HashMap();
for (int node = 1; node <= N; ++node)
dist.put(node, Integer.MAX_VALUE); //initialize with max_value
dfs(graph, K, 0); //dfs starting from node k, construct dist hashmap
int ans = 0; //get the final results
for (int cand: dist.values()) {
if (cand == Integer.MAX_VALUE) return -1;
ans = Math.max(ans, cand); //iterate all dist.values() to get the most distance
}
return ans;
}
public void dfs(Map<Integer, List<int[]>> graph, int node, int elapsed) { //this dfs function use three parameters: graph, current_node, distance from k to current node
if (elapsed >= dist.get(node)) return; //if the elapsed is larger than we areadly put in, then this is the dfs end
dist.put(node, elapsed); //only when we have smaller elapsed, will we update distance hashmap
if (graph.containsKey(node))
for (int[] info: graph.get(node)) //for every neighbot current node have
dfs(graph, info[1], elapsed + info[0]); //we dfs all the way down
}
}
//so the gernal idea of this problem is given a fixed starter, find the farthest point from this fixed points. so which means we need to find the most from all the shortest path.
//so the solution is pretty straight forward, we consstruct the graph, and then use dfs to get the shortest distance from k, and we need to store them in hashmap. and then we iterate all the value of hashmap, and get the largest value.
another way is dijkstra, and we can say that this problem is not even a modified dijkstra, it is dijkstra.
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]}); //constrauct the graph
}
dist = new HashMap();
for (int node = 1; node <= N; ++node)
dist.put(node, Integer.MAX_VALUE); //initialize dist hashmap
dist.put(K, 0); //put the starting node
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;
}
}

浙公网安备 33010602011771号