Pagini recente » Cod sursa (job #2175569) | Cod sursa (job #1030517) | Cod sursa (job #1828652) | Cod sursa (job #1951792) | Cod sursa (job #2040349)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define Nmax 50005
#define INF 2000000000
using namespace std;
int n;
priority_queue<pair<int,int>>q;
vector<pair<int,int>>L[Nmax];
int d[Nmax];
inline void init(){
for(int i=1;i<=n;i++){
d[i]=INF;
}
}
void dijkstra(){
init();
q.push(make_pair(0,1));
d[1]=0;
while(!q.empty()){
pair<int,int>p=q.top();
q.pop();
p.first=-p.first;
if(p.first != d[p.second])
continue;
for(auto it: L[p.second]){
if(d[it.first]>it.second+p.first){
d[it.first]=it.second+p.first;
q.push(make_pair(-d[it.first],it.first));
}
}
}
}
int main()
{
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int m,x,y,z;
cin>>n>>m;
for(int i=1;i<=m;++i){
cin>>x>>y>>z;
L[x].push_back(make_pair(y,z));
}
dijkstra();
for(int i=2;i<=n;++i){
if(d[i]==INF)
cout<<0<<" ";
else{
cout<<d[i]<<" ";
}
}
return 0;
}