Pagini recente » Cod sursa (job #764610) | Cod sursa (job #2508954) | Cod sursa (job #633173) | Cod sursa (job #2321501) | Cod sursa (job #3161413)
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
int n, m, u, v, c;
vector<int> dp;
vector<vector<pair<int, int>>> adj;
void citire() {
cin >> n >> m;
dp.resize(n + 1, INF);
adj.resize(n + 1);
while (m--) {
cin >> u >> v >> c;
adj[u].push_back(make_pair(v, c));
}
}
void dijkstra(int start = 1) {
priority_queue<pair<int, int>> q;
dp[start] = 0;
q.push(make_pair(start, 0));
while (!q.empty()) {
int x, y, w, z;
x = q.top().first;
y = -q.top().second;
q.pop();
if (y > dp[x])
continue;
for (auto it : adj[x]) {
w = it.first;
z = it.second;
if (dp[w] > y + z) {
dp[w] = y + z;
q.push(make_pair(w, -dp[w]));
}
}
}
}
void afis() {
for (int i = 2; i <= n; i++)
cout << (dp[i] == INF? 0 : dp[i]) << ' ';
cout << '\n';
}
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
citire();
dijkstra();
afis();
return 0;
}