The task uses a function with the Prima algorithm to build a spanning tree of the graph, however, I am told that in the algorithm itself, the selection priority of the next vertex should be done for O (log n), and for me it is done for O (n).
How to remake my algorithm to look for the next vertex for O (log n)?
PS: They say to do with the help of the priority queue, however, I do not understand where to use it here.
Code:
using namespace std; const long long inf = 1001; void My_Prima(vector<vector<pair<int, int>>> const &Vec, const int &n const&m) { vector <int> dista(n, inf); vector <bool> used(n, 0); dista[0] = 0; while (true) { int h = -1; int des = inf; for (int i = 0; i < n; i++) { if (!used[i] && (des >= dista[i])) { h = i; des = dista[i]; } } if (h == -1) { break; } used[h] = true; for (int i = 0; i < Vec[h].size(); i++) { if (!used[Vec[h][i].first]) { dista[Vec[h][i].first] = min(dista[Vec[h][i].first], Vec[h][i].second); } } } long long finsuma = 0; for (int i = 0; i < n; i++) { finsuma += dista[i]; } cout << finsuma; }