Cod sursa(job #1386359)

Utilizator EhtRalpmetFMI Ardei Claudiu-Alexandru EhtRalpmet Data 12 martie 2015 22:01:35
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
//#include <iostream>
#include <fstream>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>
#define inf 1000000000

using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");

struct nod{
    int y;
    int d;
};
struct cmp{
    bool operator()(nod a, nod b){
        return a.d>b.d;
    }
};
nod aux,bux;
int i,n,m,x,y,j;
int dist[50001];
vector<nod> v[50001];
priority_queue<nod, vector<nod>, cmp > pq;

int main(){
    cin>>n>>m;
    for(i=1;i<=m;i++){
        cin>>x>>aux.y>>aux.d;
        v[x].push_back(aux);
    }
    for(i=2;i<=n;i++){
        dist[i]=inf;
    }
    for(i=0;i<v[1].size();i++){
        pq.push(v[1][i]);
    }

    while(!pq.empty()){
        aux=pq.top();
        pq.pop();
        if(aux.d<dist[aux.y]){
            dist[aux.y]=aux.d;
            for(j=0;j<v[aux.y].size();j++){
                bux.y=v[aux.y][j].y;
                bux.d=v[aux.y][j].d+aux.d;
                pq.push(bux);
            }
        }
    }
    for(i=2;i<=n;i++){
        if(dist[i]==inf){
            cout<<0<<' ';
            continue;
        }
        cout<<dist[i]<<' ';
    }
    return 0;
}