Cod sursa(job #1370570)

Utilizator FapFapAdriana FapFap Data 3 martie 2015 15:48:04
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <set>
#include <vector>
#include <fstream>
#define nmax 50005
#define inf (1<<28)
#define value first
#define node second
using namespace std;

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

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

int number_of_nodes, number_of_edges;
int best[nmax];

void dijkstra(){
    for(int i=2; i<=number_of_nodes; i++)    best[i]= inf;
    s.insert(make_pair(0, 1));
    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] > best[current]+ neigh_val){
                best[neigh]= best[current]+ neigh_val;
                s.insert(make_pair(best[neigh], neigh));
            }
        }
    }
    for(int i=2; i<=number_of_nodes; i++)
        if(best[i]== inf)   fout << 0 << " ";
    else fout << best[i] << " ";
}

int main(){
    fin.sync_with_stdio(false);
    int x, y, w;
    fin >> number_of_nodes >> number_of_edges;
    for(int i=1; i<=number_of_edges; i++){
        fin >> x >> y >> w;
        v[x].push_back(make_pair(w, y));
    }
    dijkstra();

    return 0;
}