Pagini recente » Cod sursa (job #2072206) | Cod sursa (job #1398938) | Cod sursa (job #3128727) | Cod sursa (job #375973) | Cod sursa (job #954860)
Cod sursa(job #954860)
#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;
}