Cod sursa(job #954860)

Utilizator Pcosmin93Posteuca Cosmin Pcosmin93 Data 30 mai 2013 11:48:13
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 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,nr;
    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(graf[nod][it].second<length[graf[nod][it].first]){

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




    }
    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;
}