Pagini recente » Cod sursa (job #2323628) | Cod sursa (job #259953) | Cod sursa (job #2267957) | Cod sursa (job #3245866) | Cod sursa (job #2572814)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int main() {
int n, m; fin >> n >> m;
vector<vector<pair<int, int>>> ad(n + 1);
for (int i = 0; i < m; i++) {
int x, y, z; fin >> x >> y >> z;
ad[x].emplace_back(y, z);
}
vector<int> dp(n + 1, 1e9);
dp[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, 1);
vector<bool> vis(n + 1);
while (!pq.empty()) {
int node = pq.top().second; pq.pop();
if (!vis[node]) {
vis[node] = true;
for (auto arc : ad[node])
if (dp[node] + arc.second < dp[arc.first]) {
dp[arc.first] = dp[node] + arc.second;
pq.emplace(dp[arc.first], arc.first);
}
}
}
for (int i = 2; i <= n; i++)
fout << (dp[i] == 1e9 ? 0 : dp[i]) << ' ';
fout << '\n';
fout.close();
return 0;
}