Pagini recente » Cod sursa (job #1714787) | Cod sursa (job #1485668) | Cod sursa (job #481882) | Cod sursa (job #2441520) | Cod sursa (job #2742985)
#include <bits/stdc++.h>
using namespace std;
#define cin in
ifstream in("dijkstra.in");
#define cout out
ofstream out("dijkstra.out");
#define NMAX 500010
#define elem pair<int, int>
vector<elem> nodes[NMAX];
bitset<NMAX> parcurse;
int minim[NMAX];
int main() {
int n, m;
cin >> n >> m;
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));
}
priority_queue<elem, vector<elem>, greater<elem>> coada;
coada.push(elem(0, 1));
while (coada.size()) { // bfs
elem from = coada.top();
coada.pop();
if (parcurse[from.second])
continue;
parcurse[from.second] = 1;
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;
}