Cod sursa(job #2944807)

Utilizator radubuzas08Buzas Radu radubuzas08 Data 22 noiembrie 2022 23:04:21
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
//
// Created by Radu Buzas on 22.11.2022.
//
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

int n, m, x, y, c;
vector<pair<int, int>> graph[50001];
int d[50001];

void dijkstra(int x){
    d[x] = 0;
    priority_queue<pair<int, int>> q;
    q.push({0,x});
    while (!q.empty()){
        int cost = -q.top().first;
        int nod = q.top().second;
        q.pop();
        for (pair<int, int> k : graph[nod]) {
            if(cost + k.first < d[k.second]){
                d[k.second] = cost + k.first;
                q.push({- cost - k.first, k.second});
            }
        }
    }
}

int main(){
    in >> n >> m;
    const int inf = (((unsigned)1 << 31) -1);
    for(int i = 1; i <=n; ++i)
        d[i] = inf;
    for(int i = 1; i <= m; ++i){
        in >> x >> y >> c;
        graph[x].push_back({c, y});
//        graph[y].push_back({x, c});
    }
    dijkstra(1);
    for (int i = 2; i <= n; ++i) {
        if((d[i] ^ inf) == 0)
            out << 0 << ' ';
        else
            out << d[i] << ' ';
    }
    in.close();
    out.close();
    return 0;
}