Pagini recente » Cod sursa (job #3254259) | Cod sursa (job #1128682) | Cod sursa (job #1517428) | Cod sursa (job #2879210) | Cod sursa (job #2695970)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
const int N_MAX = 100005;
const int INF = 1e9;
std::vector<std::pair<int, int> > graph[N_MAX];
std::priority_queue<std::pair<int, int> > pq;
bool vis[N_MAX];
int dist[N_MAX];
int n, m, t_cost;
void dijkstra(int start) {
dist[start] = 0;
pq.push({0, start});
for (int i = 1; i <= n; i++) {
if (i != start) {
dist[i] = INF;
}
}
while (!pq.empty()) {
int node = pq.top().second;
pq.pop();
if (!vis[node]) {
vis[start] = true;
for (int i = 0; i < graph[node].size(); ++i) {
int next = graph[node][i].first;
int cost = graph[node][i].second;
if (dist[next] > dist[node] + cost) {
dist[next] = dist[node] + cost;
pq.push({-dist[next], next}); /// pt minim
}
}
}
}
}
int main() {
std::ifstream fin("dijkstra.in");
std::ofstream fout("dijkstra.out");
fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, cost;
std::cin >> x >> y >> cost;
graph[x].push_back({y, cost});
}
dijkstra(1);
for (int i = 2; i <= n; ++i) {
if (dist[i] != INF) {
fout << dist[i] << " ";
}
else {
fout << 0 << " ";
}
}
return 0;
}