Pagini recente » Cod sursa (job #1249847) | Cod sursa (job #2653614) | Cod sursa (job #2689035) | Cod sursa (job #2057357) | Cod sursa (job #3199775)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int main() {
int n, m;
fin >> n >> m;
std::vector<vector<pair<int, int>>> graph(n);
for (int i = 0; i < m; i++) {
int from, to, dist;
fin >> from >> to >> dist;
from--, to--;
graph[from].emplace_back(to, dist);
}
std::vector<int> dists(n, INT_MAX);
std::priority_queue<
pair<int, int>,
std::vector<pair<int, int>>,
greater<>
> path;
path.emplace( 0, 0 );
while (!path.empty()) {
pair<int, int> current = path.top(); path.pop();
for (auto& neighbour: graph[current.first]) {
if (
dists[neighbour.first] == INT_MAX ||
dists[neighbour.first] > current.second + neighbour.second
) {
dists[neighbour.first] = current.second + neighbour.second;
path.emplace( neighbour.first, current.second + neighbour.second );
}
}
}
for (int i = 1; i < n; i++)
fout << (dists[i] == INT_MAX ? 0 : dists[i]) << ' ';
return 0;
}