Pagini recente » Istoria paginii runda/5_martie_simulare_oji_2024_clasele_11_12 | Istoria paginii runda/oji2012 | Cod sursa (job #1601489) | Cod sursa (job #1724651) | Cod sursa (job #3258732)
#include <bits/stdc++.h>
using namespace std;
int n, m;
int cost[50001];
vector<vector<pair<int, int>>> v(50001);
void dijkstra() {
memset(cost, 0x3f, sizeof(cost));
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({0, 1});
cost[1] = 0;
while (!q.empty()) {
int node = q.top().second;
int pastCost = q.top().first;
q.pop();
if (pastCost == cost[node]) {
for (int i = 0; i < v[node].size(); ++i) {
if (cost[node] + v[node][i].second < cost[v[node][i].first]) {
cost[v[node][i].first] = cost[node] + v[node][i].second;
q.push({cost[v[node][i].first], v[node][i].first});
}
}
}
}
}
int main() {
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({b, c});
}
dijkstra();
for (int i = 2; i <= n; ++i) {
if (cost[i] == 0x3f3f3f3f) {
cost[i] = 0;
}
cout << cost[i] << " ";
}
}