Pagini recente » Cod sursa (job #2134641) | Cod sursa (job #2875036) | Cod sursa (job #3003759) | Cod sursa (job #3290051) | Cod sursa (job #2148885)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nMax = 50001;
int n;
int dp[nMax];
bool used[nMax];
vector <pair <int, int>> L[nMax];
priority_queue <pair <int, int>> q;
void Read() {
int m, x, y, cost;
fin >> n >> m;
for (int i = 1; i <= m; i++) {
fin >> x >> y >> cost;
L[x].push_back({y, cost});
}
fin.close();
}
void Solve() {
for (int i = 1; i <= n; i++)
dp[i] = nMax;
dp[1] = 0;
q.push({0, 1});
while (!q.empty()) {
int nod = q.top().second;
q.pop();
if (used[nod])
continue;
used[nod] = 1;
for (auto it : L[nod]) {
if(dp[it.first] > dp[nod] + it.second) {
dp[it.first] = dp[nod] + it.second;
q.push({-dp[it.first], it.first});
}
}
}
}
void Write() {
for (int i = 2; i <= n; i++) {
if (dp[i] == nMax) fout << "0 ";
else fout << dp[i] << " ";
}
fout.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}