Algorithm


C. Dijkstra?
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form aibi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.

It is possible that the graph has loops and multiple edges between pair of vertices.

Output

Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.

Examples
input
Copy
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
output
Copy
1 4 3 5 
input
Copy
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
output
Copy
1 4 3 5 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int const N = 1e5 + 1;
long long const M = 1e5 * 1e6 + 1;
int n, m, parent[N];
long long cost[N];
bool vis[N];
vector<vector<pair<int, int>>> g;
vector<int> res;
priority_queue<pair<long long, int> > qu;

void Dijkstra(int src) {
    while(!qu.empty()) qu.pop();
    for(int i = 0; i < n; i++) cost[i] = M;
    cost[src] = 0;
    qu.push(make_pair(0, src));
    
    while(!qu.empty()) {
        int v = qu.top().second;
        long long c = -qu.top().first;
        
        qu.pop();
        if(vis[v]) continue;
        vis[v] = true;
        
        for(int i = 0; i < g[v].size(); i++)
            if(g[v][i].second + c < cost[g[v][i].first]) {
                parent[g[v][i].first] = v;
                cost[g[v][i].first] = g[v][i].second + c;
                qu.push(make_pair(-(g[v][i].second + c), g[v][i].first));
            }
    }
}

int main() {
    int u, v, c;
    
    cin >> n >> m;
    g.resize(n);
    
    for(int i = 0; i < m; i++) {
        cin >> u >> v >> c;
        u--; v--;
        g[u].push_back(make_pair(v, c));
        g[v].push_back(make_pair(u, c));
    }
    
    Dijkstra(0);
    
    if(cost[n - 1] == M) cout << -1 << endl;
    else {
        v = n - 1;
        
        while (v != 0) {
            res.push_back(v + 1);
            v = parent[v];
        }
        
        res.push_back(1);

        for (int i = res.size() - 1; i >= 0; i--) cout << res[i] << ' ';
        cout << endl;
    }
    
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1

Output

x
+
cmd
1 4 3 5
Advertisements

Demonstration


Codeforcess Solution Dijkstra C,C++, Java, Js and Python ,Dijkstra,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+