Pagini recente » Cod sursa (job #774759) | Cod sursa (job #2866150) | Cod sursa (job #1387906) | Cod sursa (job #530549) | Cod sursa (job #2040330)
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <queue>
#include <vector>
#define Nmax 50005
#define INF 1000000000
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;
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()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int m,x,y,z;
scanf("%d%d", &n,&m);
for(int i=1;i<=m;++i){
scanf("%d%d%d", &x,&y,&z);
L[x].push_back(make_pair(y,z));
}
dijkstra();
for(int i=2;i<=n;++i){
if(d[i]==INF)
printf("0 ");
else{
printf("%d ",d[i]);
}
}
return 0;
}