Pagini recente » Cod sursa (job #1962083) | Cod sursa (job #2146735) | Cod sursa (job #890302) | Cod sursa (job #1102884) | Cod sursa (job #2671848)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
vector<pair<int, int> > graph[50001];
int n, m, minRoad[50001];
bool inQueue[50001];
struct compareMethod {
bool operator() (int a, int b) const{
return minRoad[a] > minRoad[b];
}
};
priority_queue <int, vector<int>, compareMethod> q;
void dijkstraAlgorithm() {
q.push(1);
while (!q.empty()) {
int node = q.top();
q.pop();
inQueue[node] = false;
for (auto it : graph[node]) {
int currentRoad = minRoad[node] + it.second;
if (minRoad[it.first] > currentRoad) {
minRoad[it.first] = currentRoad;
if (!inQueue[it.first]) {
q.push(it.first);
inQueue[it.first] = true;
}
}
}
}
}
int main() {
fin.tie(0);
ios::sync_with_stdio(0);
fin >> n >> m;
int a, b, c;
for (int i = 0; i < m; i++) {
fin >> a >> b >> c;
graph[a].push_back(make_pair(b, c));
}
for (int i = 2; i <= n; i++)
minRoad[i] = INT_MAX;
dijkstraAlgorithm();
for (int i = 2; i <= n; i++) {
if (minRoad[i] == INT_MAX)
fout << "0 ";
else
fout << minRoad[i] << " ";
}
return 0;
}