Pagini recente » Cod sursa (job #2921334) | Cod sursa (job #1092505) | Cod sursa (job #1326586) | Cod sursa (job #567658) | Cod sursa (job #2944813)
//
// Created by Radu Buzas on 22.11.2022.
//
#include <fstream>
#include <vector>
#include <queue>
#define inf 200000005
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x, y, c;
vector<vector<pair<int, int>>> graph;
priority_queue<pair<int, int>> q;
int d[50001], v[50001];
void dijkstra(int x){
for(int i = 1; i <=n; ++i)
d[i] = inf;
d[x] = 0;
q.push(make_pair(0,x));
while (!q.empty()){
int nod = q.top().second;
int cost = d[nod];
q.pop();
if(v[nod] == 0) {
++v[nod];
int size = graph[nod].size();
for (int i = 0; i < size; ++i) {
int C = graph[nod][i].first;
int N = graph[nod][i].second;
if (cost + C < d[N]) {
d[N] = cost + C;
q.push(make_pair(-d[N], N));
}
}
}
}
}
int main(){
fin >> n >> m;
graph.resize(n+1);
for(int i = 1; i <= m; ++i){
fin >> 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)
fout << 0 << ' ';
else
fout << d[i] << ' ';
}
return 0;
}