#include <iostream>using namespace Std;const int Max = 100;int p[max][max];const int maxcost = 99;int lowcost[max];in T nearest[max];bool mark[max];void Prime (int k,int n) {memset (lowcost,, sizeof (lowcost)); memset (nearest,-1, sizeof ( nearest)); Memset (Mark, False, sizeof) nearest[k] = k;lowcost[k] = 0;mark[k] = true;//k Ed the setfor (int i = 0; i < n; i++) {//update lowcost and nearestfor (int j = 0; J < N; j + +) {if (p[k][j]! = 0 & &!mark[j] && lowcost[j] > P[k][j]) {lowcost[j] = p[k][j];nearest[j] = k;}} Find the min lowcostint min = maxcost;for (int j = 0; J < N; j + +) {if (!mark[j] && min > Lowcost[j]) {min = Lowcost[j];k = j;}} Mark[k] = true;}} int main () {freopen ("Sample_input.txt", "R", stdin), int vertex, edge;cin >> edge >> vertex;int u, V, cost;for (i NT i = 0; i < vertex; i++) {cin >> u >> v >> cost;p[u][v] = cost;p[v][u] = Cost;} Prime (0, Edge); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Minimum cost spanning Tree