Pagini recente » Cod sursa (job #1644839) | Cod sursa (job #1097359) | Cod sursa (job #1645861) | Cod sursa (job #2256755) | Cod sursa (job #2743002)
#include <bits/stdc++.h>
using namespace std;
#define elem pair<int, int>
#define cin in
ifstream in("dijkstra.in");
#define cout out
ofstream out("dijkstra.out");
int main() {
int n, m;
cin >> n >> m;
vector<vector<elem>> nodes(n + 1);
while (m--) { // add nodes
int a, b, lg;
cin >> a >> b >> lg;
nodes[a].push_back(elem(lg, b));
}
vector<int> minim(n + 1, -1);
priority_queue<elem, vector<elem>, greater<elem>> coada;
coada.push(elem(0, 1));
minim[1] = 0;
while (coada.size()) { // bfs
elem from = coada.top();
coada.pop();
for (elem act : nodes[from.second]) {
if (minim[act.second] < 0 || from.first + act.first < minim[act.second]) {
coada.push(elem(from.first + act.first, act.second));
minim[act.second] = from.first + act.first;
}
}
}
for (int i = 2; i <= n; i++) // show
cout << minim[i] << ' ';
return 0;
}