Dijkstra: Shortest Reach 2
Difficulty: Hard

Given an undirected graph and a starting node, determine the lengths of the shortest paths from the starting node to all other nodes in the graph.  If a node is unreachable, its distance is -1.  Nodes will be numbered consecutively from $1$ to $n$, and edges will have varying distances or lengths.
 
For example, consider the following graph of 5 nodes:

```
Begin	End	Weight
1	2	5
2	3	6
3	4	2
1	3	15
```
[##]: "![image](https://s3.amazonaws.com/hr-assets/0/1523465555-dcd300c21d-drawing.jpg)" 
![image](https://s3.amazonaws.com/hr-assets/0/1535120384-c96f8f586f-djikstraexample.png)   
Starting at node $1$, the shortest path to $2$ is direct and distance $5$.  Going from $1$ to $3$, there are two paths: $1\rightarrow2\rightarrow3$ at a distance of $5+6=11$ or $1\rightarrow3$ at a distance of $15$.  Choose the shortest path, $11$.  From $1$ to $4$, choose the shortest path through $3$ and extend it: $1\rightarrow2\rightarrow3\rightarrow4$ for a distance of $11+2=13$  There is no route to node $5$, so the distance is $-1$.

The distances to all nodes in increasing node order, omitting the starting node, are `5 11 13 -1`.

**Function Description**  

Complete the *shortestReach* function in the editor below.  It should return an array of integers that represent the shortest distance to each node from the start node in ascending order of node number.  

shortestReach has the following parameter(s):  

- *n*: the number of nodes in the graph  
- *edges*: a 2D array of integers where each $edges[i]$ consists of three integers that represent the start and end nodes of an edge, followed by its length  
- *s*: the start node number  

Input Format:
The first line contains $t$, the number of test cases. 

Each test case is as follows:  
- The first line contains two space-separated integers $n$ and $m$, the number of nodes and edges in the graph.  
- Each of the next $m$ lines contains three space-separated integers $x$, $y$, and $r$, the beginning and ending nodes of an edge, and the length of the edge.  
- The last line of each test case has an integer $s$, denoting the starting position.  




Output Format:
For each of the $t$ test cases, print a single line consisting $n-1$ space separated integers denoting the shortest distance to the $n-1$ nodes from starting position $s$ in  increasing order of their labels, excluding $s$. 

For unreachable nodes, print $-1$.

Sample Input:
    1
    4 4
    1 2 24
    1 4 20
    3 1 3
    4 3 12
    1

Sample Output:
    24 3 15

Python 3 starter code:
#
# Complete the 'shortestReach' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER n
#  2. 2D_INTEGER_ARRAY edges
#  3. INTEGER s
#

def shortestReach(n, edges, s):
    # Write your code here




Reference solution (C++):
###C++  

```cpp  
#define INF 99999999

#define pb push_back

#define mp make_pair

using namespace std;

vector<pair<'int','int'> >adj[10009];

vector<int> dijkstra(vector<int>d,int s,int n) {
    d.clear();

    d.resize(n+1);
    for(int i=0;i<=n;i++)
    d[i]=INF;
    set<pair<int,int> >q;
    q.clear();
    bool vis[n+2];
    memset(vis,false,sizeof(vis));
    vis[s]=true;
    d[s]=0;
    q.insert(mp(0,s));
    while(!q.empty())
    {
        pair<int,int> top = *q.begin();
        q.erase(q.begin());
        int u=top.second;
        vector<pair<int,int> >::iterator it;
        for(it=adj[u].begin();it!=adj[u].end();it++)
        {
            int v=it->second;
            int cost=it->first;
            if(d[v]>d[u]+cost)
            {
                if(d[v]!=INF)
                {
                    q.erase(q.find(mp(d[v],v)));
                }
                d[v]=d[u]+cost;
                q.insert(mp(d[v],v));
            }

        }
    }
    return d;
}

int main() 

{
    
int T,r,x,y,n,m,i,s,d;
    
cin>>T;
while(T--)
{
    cin>>n>>m;
    for(i=0;i<=n+9;i++)
    adj[i].clear();
    vector<int>d1;
    d1.resize(n+1);
    for(i=0;i<m;i++)
    {
        cin>>x>>y>>r;
        adj[x].pb(mp(r,y));
        adj[y].pb(mp(r,x));
    }
    cin>>s;
    d1=dijkstra(d1,s,n);
    for(i=1;i<=n;i++)
    {
        if(i!=s)
        {
            if(d1[i]!=INF)
            cout<<d1[i]<<" ";
            else
            cout<<"-1 ";
        }
        
    }
    cout<<endl;
}
return 0;
}
```