<p>You are given a network of <code>n</code> nodes, labeled from <code>1</code> to <code>n</code>. You are also given <code>times</code>, a list of travel times as directed edges <code>times[i] = (u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>)</code>, where <code>u<sub>i</sub></code> is the source node, <code>v<sub>i</sub></code> is the target node, and <code>w<sub>i</sub></code> is the time it takes for a signal to travel from source to target.</p>

<p>We will send a signal from a given node <code>k</code>. Return <em>the <strong>minimum</strong> time it takes for all the</em> <code>n</code> <em>nodes to receive the signal</em>. If it is impossible for all the <code>n</code> nodes to receive the signal, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/23/931_example_1.png" style="width: 217px; height: 239px;" />
<pre>
<strong>Input:</strong> times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> times = [[1,2,1]], n = 2, k = 1
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> times = [[1,2,1]], n = 2, k = 2
<strong>Output:</strong> -1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= times.length &lt;= 6000</code></li>
	<li><code>times[i].length == 3</code></li>
	<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
	<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
	<li><code>0 &lt;= w<sub>i</sub> &lt;= 100</code></li>
	<li>All the pairs <code>(u<sub>i</sub>, v<sub>i</sub>)</code> are <strong>unique</strong>. (i.e., no multiple edges.)</li>
</ul>

class Solution {
public:
    class State{
        public:
        //图中节点的ID
        int id;
        //从start节点到当前节点的距离
        int disFromStart;
        State(int id, int disFromStart){
            this->id=id;
            this->disFromStart=disFromStart;
        }
    };

    struct Comp{//comp和下面dikstra是并列关系，在下面的函数中进行调用，所以comp不能写在下面函数的里面
        bool operator()(const State& a, const State& b){
            return a.disFromStart > b.disFromStart;
        }
    };

    //输入一个起点start，计算从start到其他节点的最短距离
    vector<int> dijkstra (int start, vector<vector<vector<int>>> graph){//list映射数组
        //disTo[i]的值就是起点start到节点i的最短路径权重
        vector<int> disTo(graph.size(),INT_MAX);
        //base case ,start 到 start的最短距离就是0
        disTo[start]=0;
        //优先级队列，disFromStart较小的排在前面
        priority_queue<State, vector<State>, Comp> pq;
        
        //从起点start开始进行BFS
        State start_item = State(start,0);
        pq.push(start_item);

        while(!pq.empty()){
            State curState=pq.top();
            pq.pop();//别忘记pop
            int curNodeId = curState.id;
            int curDistFromStart= curState.disFromStart;
            if(curDistFromStart > disTo[curNodeId]){
                continue;
            }
            // 将curNode的相邻节点装入队列
            for(vector<int> neighbor : graph[curNodeId]){
                int nextNodeId= neighbor[0];
                int disToNextNode= disTo[curNodeId]+neighbor[1];
                //更新
                if(disTo[nextNodeId] > disToNextNode){
                    disTo[nextNodeId] = disToNextNode;
                    pq.push(State(nextNodeId, disToNextNode));
                }
            }
        }
        return disTo;

    }
    int networkDelayTime(vector<vector<int>>& times, int n, int k) {
        //节点编号从1开始，所以邻接表大小为n+1
        vector<vector<vector<int>>> graph(n+1);
        //构造图
        for(vector<int> edge : times){
            int from= edge[0];
            int to= edge[1];
            int weight=edge[2];
            //邻接表存储图结构，同时存储权重信息
            //from->list<(to,weight)>;
            graph[from].push_back(vector<int>{to,weight});
        }
        //启动dijkstra算法计算以节点K为起点到其他节点的最短距离
        vector<int> disTo=dijkstra(k, graph);
        //找到最长的那一条最短路径
        int res=0;
        for(int i=1; i<disTo.size();i++){
            if(disTo[i]== INT_MAX){
                //说明有节点不可达，返回-1
                return -1;
            }
            res=max(res, disTo[i]);
        }
        return res;
    }
    

};
66203