Cod sursa(job #3125880)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 4 mai 2023 18:47:29
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;

const int MAX = 5e4 + 1;
const int inf = 1e18+1;

ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");

int dp[MAX], n , m , x , y , c , pre[MAX];

bool b[MAX];

vector <int> drum;

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 = 1 ; i <= n ; i++){

        dp[i] = inf;
    }

    dp[1] = 0;

    pq.push(1);

    while(!pq.empty()){

        x = pq.top();
        pq.pop();

        if(!b[x]){

            b[x] = 1;

        }else continue;

        for(auto it : g[x]){

            if(dp[it.y] > dp[x] + it.c){

                dp[it.y] = dp[x] + it.c;
                pre[it.y] = x;

                pq.push(it.y);
            }
        }
    }

    for(int i = 2 ; i <= n ; i++){

        if(dp[i] == inf) dp[i] = 0;

        cout << dp[i] << ' ';
    }

    return 0;
}