Cod sursa(job #2283378)

Utilizator DordeDorde Matei Dorde Data 15 noiembrie 2018 14:47:21
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
struct code {
    int node , cost;
    bool operator > (code r) const {
        return cost > r . cost;
    }
};
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
int const NM = 25e4;
vector <code> v [1 + NM];
int dp [1 + NM];
bool viz [1 + NM];
priority_queue <code , vector <code> , greater <code> > q;
void shortest (){
    q . push ({1 , 0});
    while (! q . empty ()){
        code x = q . top ();
        viz [x . node] = true;
        while (q . size () && viz [q . top () . node])
            q . pop ();
        for(auto i : v [x . node]){
            if (dp [i . node] > dp [x . node] + i . cost ){
                dp [i . node] = dp [x . node] + i . cost;
                q . push ({i . node , dp [i . node]});
            }
        }
    }
}
int main()
{
    int n , m;
    cin >> n >> m ;
    for(int i = 1 ; i <= m ; ++ i){
        int a , b , c;
        cin >> a >> b >> c;
        v [a] . push_back ({b , c});
        v [b] . push_back ({a , c});
    }
    fill (dp + 2 , dp + n + 1 , (1 << 30));
    shortest ();
    for(int i = 2 ; i <= n ; ++ i)
        cout << dp [i] << ' ';
    return 0;
}