<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:
    int maxn = 101;
    int maxm = 6001;

    vector<int> head;
    vector<int> next;
    vector<int> to;
    vector<int> weight;
    int cnt = 1;

    vector<int> heap;
    vector<int> where;
    int heapsize = 0;

    vector<int> dis;

    void build(){
        head.assign(maxm, 0);
        next.assign(maxm, 0);
        to.assign(maxm, 0);
        weight.assign(maxm, 0);
        cnt = 1;

        heap.assign(maxn, 0);
        where.assign(maxn, -1);
        heapsize=0;

        dis.assign(maxn, INT_MAX); 

    }

    void addEdge(int u, int v, int w){

        next[cnt] = head[u];
        to[cnt] = v;
        weight[cnt] = w;
        head[u] = cnt;
        cnt++;

    }
    void addOrUpdateOrIgnore(int v, int c){

        if(where[v] == -1){
            heap[heapsize] = v; 
            where[v] = heapsize;
            heapsize++;

            dis[v]=c;

            heapInsert(where[v]);
        }
        else if(where[v] == -2) return;
        else{
            dis[v] = min(dis[v], c);
            heapInsert(where[v]);  
        }
    }
    int pop(){
        int res = heap[0];
        swap(0, heapsize-1);
        heapsize--;
        heapify(0);
        where[res] = -2;

        return res;
    }
    void heapify(int i){
        int l = 1; //?
        // int l = i*2+1;
        while(l<heapsize){
            int best = l+1 < heapsize && dis[heap[l+1]] < dis[heap[l]]? l+1: l;
            best = dis[heap[best]] < dis[heap[i]]? best: i;
            if(best==i) return;
            swap(best, i);
            i = best;
            l = i*2+1;
        }
    }
    void heapInsert(int i){
        while(dis[heap[i]] <dis[heap[(i-1)/2]]){
            swap(i, (i-1)/2);
            i = (i-1)/2;
        }
    }
    void swap(int i, int j){
        int temp = heap[i];
        heap[i] = heap[j];
        heap[j] = temp;
        where[heap[i]] = i;
        where[heap[j]] = j;
        
    }

    bool isEmpty(){
        return heapsize == 0;
    }

    int networkDelayTime(vector<vector<int>>& times, int n, int k) {

        build();

        for(vector<int> vec: times){
            int u = vec[0];
            int v = vec[1];
            int w = vec[2];
            addEdge(u, v, w);
        }

        addOrUpdateOrIgnore(k, 0); //

        while(!isEmpty()){
            int u = pop();
            int edge = head[u];
            while(edge){
                int v = to[edge];
                int w = weight[edge];
                addOrUpdateOrIgnore(v, dis[u]+w); //
                edge = next[edge];
            }
        }

        int res = INT_MIN;
        for(int i = 1; i <= n; i++){
            if(dis[i] == INT_MAX) return -1;
            res = max(res, dis[i]);
        }

        return res;
    }
};
49971