Dijkstra Set Algorithm

The Dijkstra Set Algorithm is a popular graph search algorithm invented by the renowned computer scientist Edsger W. Dijkstra in 1956. It is a highly efficient algorithm designed to find the shortest path between two nodes in a graph with non-negative edge weights. The main idea behind this algorithm is to iteratively select the node with the smallest distance from the source node, update the distances to its neighbors, and add it to a set of visited nodes. This process continues until the destination node is visited or all nodes in the graph have been explored, ensuring the shortest path is found. The Dijkstra Set Algorithm begins by initializing the distance to the source node as zero and the distances to all other nodes as infinity, as these distances are unknown at the start. The algorithm uses a priority queue or a set to keep track of unvisited nodes, sorted by their distance from the source node. In each iteration, the node with the smallest distance is chosen, and its neighbors' distances are updated if a shorter path is discovered through the current node. Once a node is visited, it is removed from the priority queue or set, ensuring each node is visited only once. The algorithm terminates when either the destination node is visited or no more nodes are left in the queue, at which point the shortest path can be reconstructed by tracing back from the destination node to the source node through the updated distances.
/**************************************************************************************
    
     Dijkstra on set for sparse graphs - O(MlogM). Slower than heap version
     Based on problem 20C from codeforces: http://codeforces.ru/contest/20/problem/C

**************************************************************************************/

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>

using namespace std;

const long long INF = (long long)1e12;
const int MAXN = 105000;

struct edge {
    int to, w;
};

int n, m;
vector <edge> g[MAXN];
long long dist[MAXN];
int par[MAXN];
set < pair<long long, int> > s;
vector <int> ans;
edge e;

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++) {
        int a, b, w;
        scanf("%d %d %d", &a, &b, &w);
        e.to = b; e.w = w;
        g[a].push_back(e);
        e.to = a;
        g[b].push_back(e);
    }

    s.insert(make_pair(0, 1));
    for (int i = 2; i <= n; i++) {
        dist[i] = INF;
        s.insert(make_pair(INF, i));
    }

    while (!s.empty()) {
        int cur = s.begin()->second;
        long long cur_dist = s.begin()->first;
        s.erase(s.begin());
        for (int i = 0; i < (int) g[cur].size(); i++) {
            int to = g[cur][i].to, w = g[cur][i].w;
            if (cur_dist + w < dist[to]) {
                s.erase(make_pair(dist[to], to));
                dist[to] = cur_dist + w;
                par[to] = cur;
                s.insert(make_pair(dist[to], to));
            }
        }
    }

    if (dist[n] == INF) {
        printf("-1");
        return 0;
    }

    int cur = n;
    while (par[cur] != 0) {
        ans.push_back(cur);
        cur = par[cur];
    }
    ans.push_back(1);

    reverse(ans.begin(), ans.end());
    for (int i = 0; i < (int) ans.size(); i++)
        printf("%d ", ans[i]);

    return 0;
}

LANGUAGE:

DARK MODE: