Cod sursa(job #3269927)

Utilizator Iustin_Mircea2010Iustin Mircea Iustin_Mircea2010 Data 21 ianuarie 2025 15:08:20
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[250005];
int d[250005];
set<pair<int, int>> s;
#define INF 900000000
int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    d[1] = 0;
    for(int i = 2; i <= n; i++)
        d[i] = INF;
    s.insert({0, 1});
    while(!s.empty()){
        int nod = s.begin()->second;
        s.erase(s.begin());
        for(auto p : v[nod]){
            int to = p.first;
            int len = p.second;
            if(d[to] > d[nod] + len){
                s.erase(p);
                d[to] = d[nod] + len;
                s.insert({d[to], to});
            }
        }
    }
    for(int i = 2; i <= n; i++){
        cout << d[i] << " ";
    }
    return 0;
}