Cod sursa(job #937418)

Utilizator Pcosmin93Posteuca Cosmin Pcosmin93 Data 10 aprilie 2013 11:49:36
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>

#define INFINIT 64000
#define MAX 50001

using namespace std;

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

vector < pair < int , int > > graf[MAX];

void dijkstra(int inceput,int n){

    unsigned int length[MAX],it;
    int nod,i;
    queue <int> queue_graf;

    for( i = 1 ; i <= n ; i++ ){
        length[i]=INFINIT;
    }

    length[inceput]=0;
    queue_graf.push(inceput);

    while(queue_graf.empty()==0){

        nod=queue_graf.front();
        queue_graf.pop();

        for( it = 0 ; it < graf[nod].size() ; it++ ){

            if(length[nod]+graf[nod][it].second<length[graf[nod][it].first]){

                length[graf[nod][it].first]=length[nod]+graf[nod][it].second;
                queue_graf.push(graf[nod][it].first);
            }

        }

    }
    for( i = 1 ; i <= n ; i++ ){

        if(length[i]==INFINIT)
            length[i]=0;

        if(inceput!=i)
            out<<length[i]<<" ";

    }

}

int main(){
    int A,B,C,n,m;

    in>>n>>m;
    for( int j = 1 ; j <= m ; j++ ){

            in>>A>>B>>C;
            graf[A].push_back(make_pair(B, C));

        }

    dijkstra(1,n);

    return 0;
}