Pagini recente » Cod sursa (job #794782) | Cod sursa (job #2562570) | Cod sursa (job #1751752) | Cod sursa (job #1297385) | Cod sursa (job #3199791)
#include <fstream>
#include <set>
#include <vector>
#include <climits>
#define MAXN 50000
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);
}
int dists[MAXN] = {0};
set<pair<int, int>> path;
path.emplace( 0, 0 );
while (!path.empty()) {
pair<int, int> current = *path.begin();
path.erase(path.begin());
for (auto& neighbour: graph[current.second]) {
if (dists[neighbour.first] > current.first + neighbour.second) {
dists[neighbour.first] = current.first + neighbour.second;
path.emplace(current.first + neighbour.second, neighbour.first);
}
}
}
for (int i = 1; i < n; i++)
fout << (dists[i] == INT_MAX ? 0 : dists[i]) << ' ';
return 0;
}