Pagini recente » Cod sursa (job #1789370) | Cod sursa (job #47120) | Cod sursa (job #2475106) | Cod sursa (job #1371582) | Cod sursa (job #2742994)
#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));
nodes[b].push_back(elem(lg, a));
}
vector<int> minim(n + 1, INT_MAX);
priority_queue<elem, vector<elem>, greater<elem>> coada;
coada.push(elem(0, 1));
while (coada.size()) { // bfs
elem from = coada.top();
coada.pop();
if (minim[from.second] <= from.first)
continue;
minim[from.second] = from.first;
for (elem act : nodes[from.second])
coada.push(elem(from.first + act.first, act.second));
}
for (int i = 2; i <= n; i++) // show
cout << minim[i] << ' ';
return 0;
}