Pagini recente » Cod sursa (job #2372322) | Cod sursa (job #3257602) | Cod sursa (job #629654) | Cod sursa (job #465689) | Cod sursa (job #614704)
Cod sursa(job #614704)
#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
ifstream in("dijkastra.in");
ofstream out("dijkastra.out");
priority_queue<pair<int,int> > h[50010];
int n,m,dist[50010];
void bf(int nod) {
while(!h[nod].empty()) {
if(dist[nod] + h[nod].top().first < dist[h[nod].top().second]) {
dist[h[nod].top().second]=dist[nod] + h[nod].top().first;
bf(h[nod].top().second);
}
h[nod].pop();
}
}
int main() {
int i,a,b,c;
in >> n >> m;
for(i=1;i<=m;++i) {
in >> a >> b >> c;
h[a].push(make_pair<int,int>(c,b));
}
for(i=2;i<=n;++i)
dist[i]=1<<20;
bf(1);
for(i=2;i<=n;++i) {
if(dist[i]==(1<<20))
dist[i]=0;
out << dist[i] << " ";
}
return 0;
}