Cod sursa(job #1312634)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 9 ianuarie 2015 19:55:02
Problema Algoritmul lui Dijkstra Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <set>
#include <vector>
#include <fstream>
#define nmax 50001
#define node first
#define weight second
#define inf (1<<30)
using namespace std;

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

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

int n, m;
int best[nmax];

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

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