Pagini recente » Cod sursa (job #1555301) | Cod sursa (job #307268) | Cod sursa (job #2875722) | Cod sursa (job #1252494) | Cod sursa (job #614722)
Cod sursa(job #614722)
#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.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;
}