Cod sursa(job #3360130)

Utilizator Andrei_GAndreiG Andrei_G Data 9 iulie 2026 12:21:02
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.72 kb
#include <fstream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#define int long long
//#define int short
using namespace std;

const int nmax = 5e4;
const int inf = 2e9;

int n, m, rez[nmax + 5];
vector<pair<int, int>> adj[nmax + 5];

struct cmp{
    bool operator()(const pair<int, int>& a, const pair<int, int>& b) const{
        return a.second > b.second;
    }
};

void dijsktra(int startnode){
    priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
    rez[startnode] = 0;
    pq.push({startnode, 0});
    while (!pq.empty()){
        int curnode = pq.top().first, curweight = pq.top().second;
        pq.pop();
        if (rez[curnode] < curweight){
            continue;
        }
        for (auto& [node, weight] : adj[curnode]){
            int newweight = weight + curweight;
            if (rez[node] > newweight){
                rez[node] = newweight;
                pq.push({node, newweight});
            }
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("dijkstra,in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);
    cin>>n>>m;
    while (m--){
        int u, v, w;
        cin>>u>>v>>w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    for (int i = 1; i <= n; i++){
        rez[i] = inf;
    }
    dijsktra(1);
    for (int i = 2; i <= n; i++){
        cout<<rez[i]<<" ";
    }
}