Pagini recente » Cod sursa (job #2749870) | Cod sursa (job #2080338) | Cod sursa (job #2121350) | Cod sursa (job #2877450) | Cod sursa (job #3161431)
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
struct Node {
int u, c;
bool operator < (const Node& a) const {
return a.c < c;
}
};
int n, m, u, v, c;
vector<vector<Node>> adj;
vector<int> dp;
void citire() {
cin >> n >> m;
adj.resize(n);
dp.resize(n, INF);
while (m--) {
cin >> u >> v >> c;
u--; v--;
adj[u].push_back({v, c});
}
}
void dijkstra(int start = 0) {
priority_queue<Node> q;
q.push({start, 0});
dp[start] = 0;
while (!q.empty()) {
Node w = q.top();
q.pop();
if (w.c > dp[w.u])
continue;
for (auto child : adj[w.u])
if (child.c + w.c < dp[child.u]) {
dp[child.u] = child.c + w.c;
q.push({child.u, dp[child.u]});
}
}
}
void afis() {
for (int i = 1; i < n; i++)
cout << (dp[i] == INF? 0 : dp[i]) << ' ';
cout << '\n';
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
citire();
dijkstra();
afis();
return 0;
}