Cod sursa(job #2742985)

Utilizator teofilotopeniTeofil teofilotopeni Data 22 aprilie 2021 13:32:31
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#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;
}