Pagini recente » Cod sursa (job #911419) | Cod sursa (job #3211223) | Cod sursa (job #675016) | Cod sursa (job #676655) | Cod sursa (job #3161412)
#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;
set<pair<int, int>> s;
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) {
dp[start] = 0;
s.insert({start, 0});
while (!s.empty()) {
int x, y, w, z;
x = s.begin()->first;
y = s.begin()->second;
s.erase({x, y});
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;
s.insert({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;
}