LeetCode network delay time (shortest path problem of graph)

Posted by smeee_b on Mon, 18 Nov 2019 17:24:04 +0100

Problem Description:

There are N network nodes marked 1 to N.

Given a list of times, it indicates the time of signal passing through the directed edge times[i] = (u, v, w), where u is the source node, v is the target node, and W is the time when a signal passes from the source node to the target node.

Now, we send a signal to the current node K. How long does it take for all nodes to receive signals? If you cannot signal all nodes, return - 1.

Be careful:

The range of N is between [1, 100].
The range of K is between [1, N].
The length of times is between [1, 6000].
All edges times[i] = (u, v, w) have 1 < = u, V < = n and 0 < = w < = 100.

Thought analysis:

First of all, we should understand that the time from k to all = max (the minimum time from k to point 1, the minimum time from k to 2 The minimum time from k to n) because the transmission process is simultaneous. So this problem is transformed into the shortest path problem of graph.
Please browse. The shortest path of graph: Floyd, DisjKstra, SPFA algorithm

We first use DisjKstra algorithm to find the shortest distance from each point to k, and then find the maximum of these distances.

//DisjKstra algorithm
class Solution {
public:
    int networkDelayTime(vector<vector<int>>& times, int N, int K) {
        queue<int> myQue;//The queue is used for the node to be accessed
        vector<int> distToK(N + 1, INT_MAX);//distToK[index] represents the shortest distance between point K and point index
        vector<vector<int>> graph(N+1,vector<int>(N+1,-1));//adjacency matrix
        for(auto &time:times){//Constructing adjacency matrix
            graph[time[0]][time[1]] = time[2];
        }
        myQue.push(K);
        distToK[K] = 0;//K's shortest distance to itself is 0
        //Start searching for the shortest distance from each point to k
        while(!myQue.empty()){
            int front = myQue.front();
            myQue.pop();
            //Using the current front node, try the shortest distance from sparse point k to all nodes
            for(int target = 1; target <= N; ++target){
                if(graph[front][target] != -1 && distToK[front] + graph[front][target] < distToK[target]){
                    //If front to target has an edge, and the distance between point k and front distToK[front] + the distance between point k and target graph[front][target] is less than the distance between point k and target distToK[target]
                    distToK[target] = distToK[front] + graph[front][target];//Sparse
                    myQue.push(target);//Put in queue
                }
            }
        }
        //Find the maximum value of the shortest distance from point k to each point
        int maxRes = 0;
        for(int i = 1; i <= N; ++i){
            maxRes = max(maxRes, distToK[i]);
        }
        return maxRes == INT_MAX? -1 : maxRes;
    }
};

Topics: network less