Pagini recente » Cod sursa (job #2453641) | Cod sursa (job #1227858) | Cod sursa (job #2015143) | Cod sursa (job #433868) | Cod sursa (job #2280184)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
const int NMax = 5e4 + 5;
const int INF = 1e9;
vector < pair < int, int > > G[NMax];
int D[NMax];
void Dijkstra() {
priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > pq;
pq.emplace(0, 1);
while(!pq.empty()) {
int node = pq.top().second;
int value = pq.top().first;
pq.pop();
if(D[node] != value) continue;
for(auto it: G[node]) {
if(value + it.second < D[it.first]) {
D[it.first] = value + it.second;
pq.emplace(D[it.first], it.first);
}
}
}
}
int main(){
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; i++) {
int a, b, c;
fin >> a >> b >> c;
G[a].push_back(make_pair(b, c));
}
for(int i = 2; i <= n; i++) D[i] = INF;
Dijkstra();
for(int i = 2; i <= n; i++) {
if(D[i] == INF) {
fout << "0 ";
} else {
fout << D[i] << " ";
}
}
return 0;
}