vector used (n);
vector min_e (n, INF), sel_e (n, -1); //elementlar soni n bo'lgan vector e'lon qilish va barchasini bir xil qiymat bilan to'ldirish
min_e[0] = 0;
vector < pair > res;
long long cost = 0;
for (int i=0; iint v = -1;
for (int j=0; jif (!used[j] && (v == -1 || min_e[j] < min_e[v]))
v = j;
if (min_e[v] == INF) {
cout << "Minimal daraxt mavjud e'mas, ya'ni graf bog'lanmagan!";
exit(0);
}
used[v] = true;
if (sel_e[v] != -1) {
res.push_back (make_pair (v, sel_e[v]));
cost += min_e[v];
}
for (int to=0; toif (g[v][to] > 0 && g[v][to] < min_e[to]) {
min_e[to] = g[v][to];
sel_e[to] = v;
}
}
for (int i = 0; i < n; i++) {
cout << min_e[i] << " ";
}
cout << endl;
cout << "Minamal qoldiq daraxt qirralari summasi: " << cost << endl;
cout << "Qolgan qirralar: " << endl;
for (int i = 0; i < res.size(); i++) {
cout << res[i].first<<" "<}
}
Topshiriqlar: Graflarda “Xasis ” algoritmlar. Kruskal va Prima algoritmlari.
Berilgan graf uchun minimal qoldiq daraxt(graf skleti) aniqlansin
1.