Cod sursa(job #2259816)

Utilizator rnqftwcalina florin daniel rnqftw Data 13 octombrie 2018 20:07:18
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include<bits/stdc++.h>

using namespace std ;

#define cin in
#define cout out

priority_queue<pair<int,int>  , vector<pair<int,int>> , greater <pair<int,int>>> q ;
vector<pair<int,int>> g[50010];
int rez[50010];

void dijkstra(){
    q.push(make_pair(0,1));
    while(q.size()){
        pair<int,int> x = q.top();
        q.pop();
        if(rez[x.second] != 1 << 30)
            continue;
        rez[x.second] = x.first;
        for(auto it : g[x.second]){
            q.push(make_pair(x.first+it.second, it.first));

        }
    }

}

int main(){

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

    int n , m ;
    cin >> n >> m ;

    for(int i = 0 ; i < m ; i ++ )
    {
        int x, y , c ;
        cin >> x >> y >> c ;
        g[x].push_back(make_pair(y,c));
    }

    for(int i = 1 ; i <= n ; i ++)
        rez[i] = 1 << 30 ;

    dijkstra();

    for(int i = 2 ; i <= n ; i ++)
        if(rez[i] != 1 << 30)
            cout << rez[i] << " ";
        else
            cout << "0 " ;

}