Pagini recente » Cod sursa (job #2901455) | Cod sursa (job #162961) | Cod sursa (job #2784774) | Cod sursa (job #2919851) | Cod sursa (job #937404)
Cod sursa(job #937404)
#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 it;
int nod,i;
queue <int> queue_graf;
unsigned int length[MAX];
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;
}