Pagini recente » Cod sursa (job #1913924) | Cod sursa (job #246068) | Cod sursa (job #2323360) | Cod sursa (job #358629) | Cod sursa (job #3218738)
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <utility>
#include <vector>
#include <queue>
typedef std::pair<int32_t, int32_t> pair;
typedef std::vector<pair> vector;
typedef std::priority_queue<pair, vector, std::greater<pair>> queue;
const int32_t MAX_N = 50000;
const int32_t MAX_COST = 1000000000;
int32_t cost[MAX_N];
vector adj[MAX_N];
queue q;
int main() {
std::ifstream fin("djikstra.in");
std::ofstream fout("djikstra.out");
int32_t n, m;
fin >> n >> m;
for(int32_t i = 0; i != m; ++i) {
int32_t a, b, c;
fin >> a >> b >> c;
--a; --b;
adj[a].push_back({ b, c });
}
for(int32_t i = 1; i != n; ++i)
cost[i] = MAX_COST;
q.push({ 0, 0 });
while(!q.empty()) {
auto val = q.top();
q.pop();
int32_t nodeCost = val.first;
int32_t ind = val.second;
if(cost[ind] < nodeCost)
continue;
for(auto next : adj[ind]) {
int32_t newCost = nodeCost + next.second;
if(newCost >= cost[next.first])
continue;
cost[next.first] = newCost;
q.push({ newCost, next.first });
}
}
for(int32_t i = 1; i != n; ++i) {
if(cost[i] == MAX_COST) {
fout << "0 ";
} else {
fout << cost[i] << ' ';
}
}
fin.close();
fout.close();
return 0;
}