Cod sursa(job #2742994)

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