Cod sursa(job #3125878)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 4 mai 2023 18:40:11
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int MAX = 5e4 + 1;
const int inf = 25e4*2e4+1;
int dp[MAX], n, m, x, y, c;
struct crit{
    bool operator()( int a, int b ){
        return dp[a] > dp[b];
    }
};
priority_queue <int,vector<int>,crit> pq;
struct muchie{ int y, c;};
vector <muchie> g[MAX];

signed main(){

    cin >> n >> m;

    while(m--){
        cin >> x >> y >> c;
        g[x].push_back({y,c});
        g[y].push_back({x,c});
    }

    for(int i = 2 ; i <= n ; i++) dp[i] = inf;
    pq.push(1);
    while(!pq.empty()){
        x = pq.top();
        pq.pop();
        for(auto it : g[x]){
            if(dp[it.y] > dp[x] + it.c){
                dp[it.y] = dp[x] + it.c;
                pq.push(it.y);
            }
        }
    }
    for(int i = 2 ; i <= n ; i++){
        if(dp[i] == inf) dp[i] = 0;
        cout << dp[i] << ' ';
    }

    return 0;
}