Pagini recente » Cod sursa (job #2387627) | Cod sursa (job #2424503) | Cod sursa (job #390595) | Cod sursa (job #1689252) | Cod sursa (job #2944807)
//
// Created by Radu Buzas on 22.11.2022.
//
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, x, y, c;
vector<pair<int, int>> graph[50001];
int d[50001];
void dijkstra(int x){
d[x] = 0;
priority_queue<pair<int, int>> q;
q.push({0,x});
while (!q.empty()){
int cost = -q.top().first;
int nod = q.top().second;
q.pop();
for (pair<int, int> k : graph[nod]) {
if(cost + k.first < d[k.second]){
d[k.second] = cost + k.first;
q.push({- cost - k.first, k.second});
}
}
}
}
int main(){
in >> n >> m;
const int inf = (((unsigned)1 << 31) -1);
for(int i = 1; i <=n; ++i)
d[i] = inf;
for(int i = 1; i <= m; ++i){
in >> x >> y >> c;
graph[x].push_back({c, y});
// graph[y].push_back({x, c});
}
dijkstra(1);
for (int i = 2; i <= n; ++i) {
if((d[i] ^ inf) == 0)
out << 0 << ' ';
else
out << d[i] << ' ';
}
in.close();
out.close();
return 0;
}