Cod sursa(job #1323582)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 21 ianuarie 2015 11:52:37
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#define value first
#define node second
#define nmax 50001
#define inf (1<<30)
using namespace std;

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

set< pair<int, int> > s;
vector< pair<int, int> > v[nmax];
int best[nmax];

int n, m;

void dijkstra(){
    s.insert(make_pair(0, 1));
    for(int i=2; i<=n; i++) best[i]= inf;
    while(!s.empty()){
        int current = s.begin() -> node;
        int current_val = s.begin() -> value;
        s.erase(s.begin());
        for(int i=0; i<v[current].size(); i++){
            int neigh= v[current][i]. node;
            int neigh_val= v[current][i].value;
            if(best[neigh] > current_val + neigh_val){
                best[neigh]= current_val + neigh_val;
                s.insert(make_pair(best[neigh], neigh));
            }
        }
    }
    for(int i=2; i<=n; i++)
        if(best[i]==inf) fout << 0 << " ";
        else fout << best[i] << " ";
}


int main(){
    int x, y, w;
    fin >> n >> m;
    for(int i=1; i<=m; i++){
        fin >> x >> y >> w;
        v[x].push_back(make_pair(w, y));
    }
    dijkstra();
    return 0;
}