Pagini recente » Cod sursa (job #2275478) | Cod sursa (job #3186883) | Cod sursa (job #836079) | Cod sursa (job #995938) | Cod sursa (job #1607563)
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int nmx = 50002;
const int inf = 0x3f3f3f3f;
int n,m,dist[nmx];
vector <pair<int,int> > g[nmx];
priority_queue < pair <int,int> > q;
void citire() {
scanf("%d %d", &n, &m);
int nod1,nod2,cost;
for(int i = 1; i <= m; ++i) {
scanf("%d %d %d", &nod1, &nod2, &cost);
g[nod1].push_back(make_pair(nod2,cost));
}
}
void dijkstra(){
dist[1] = 0;
q.push(make_pair(0,1));
while(not q.empty()) {
int nod = q.top().second;
q.pop();
for(vector<pair<int,int> >::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
if(dist[it->first] > dist[nod] + it->second) {
dist[it->first] = dist[nod] + it->second;
q.push(make_pair(-dist[it->first],it->first));
}
}
}
void afish(){
for(int i = 2; i <= n; ++i)
printf("%d ", dist[i]);
printf("\n");
}
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
citire();
memset(dist,inf,sizeof(dist));
dijkstra();
afish();
return 0;
}