Cod sursa(job #2639641)

Utilizator Iustin01Isciuc Iustin - Constantin Iustin01 Data 3 august 2020 12:41:45
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>>
#include <fstream>
#define oo (1 << 30)
#define MAX 250005
using namespace std;

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

vector < pair < int , int > > g[MAX];

int d[50005], n, m, x , y, cost;
bool c[50005];

struct cmp{
    bool operator()(int a, int b){
        return d[a] > d[b];
    }
};

priority_queue < int , vector < int > , cmp > q;


void Dijkstra(int k){
    c[k] = true;
    for(int i = 1; i <= n; i++)
        d[i] = oo;
    d[k] = 0;
    q.push(k);
    while(!q.empty()){
        k = q.top();
        q.pop();
        for(int i = 0; i < g[k].size(); i++){
            int vec = g[k][i].first;
            cost = g[k][i].second;
            if(d[vec] > d[k] + cost){
                d[vec] = d[k] + cost;
                q.push(vec);
            }

        }
    }
}

int main(){
    in>>n>>m;
    for(int i = 1; i <= n; i++){
        in>>x>>y>>cost;
        g[x].push_back({y, cost});
        g[y].push_back({x, cost});
    }

    Dijkstra(1);

    for(int i = 2; i <= n; i++)
        out<<(d[i] == oo ? 0 : d[i])<<" ";
}