最小生成樹

2022-09-17 12:51:13 字數 2115 閱讀 4134

1

//本**解決,乙個圖的最小生成樹的各邊權值總和

23 #include4

using

namespace

std;5//

static宣告靜態變數,只執行一次, const限定乙個變數不允許被改變,產生靜態作用。

6static

const

int max = 100;7

static

const

int infty = (1

<<21);8

static

const

int white = 0;9

static

const

int gray = 1;10

static

const

int black = 2;11

12int

n, m[max][max];

1314

intprim()

2728 d[0] = 0;29

30while(1

) 38}39

//所有點都被遍歷結束迴圈

40if(u == -1) break;41

42 color[u] =black;

43//

更新與之相關的頂點

44for(int v = 0; v < n; v++) 51}

52}53}

5455

int sum = 0;56

for(int i = 0; i < n; i++)

59return

sum;60}

6162

intmain() 71}

7273 cout << prim() <74return0;

75}

76/*775

78-1 2 3 1 -1

792 -1 -1 4 -1

803 -1 -1 1 1

811 4 1 -1 3

82-1 -1 1 3 -1

83*/

1

//本**解決,乙個圖的最小生成樹的各邊權值總和

23 #include4

using

namespace

std;5//

static宣告靜態變數,只執行一次, const限定乙個變數不允許被改變,產生靜態作用。

6static

const

int max = 100;7

static

const

int infty = (1

<<21);8

static

const

int white = 0;9

static

const

int gray = 1;10

static

const

int black = 2;11

12int

n, m[max][max];

1314

intprim()

2728 d[0] = 0;29

30while(1

) 38}39

//所有點都被遍歷結束迴圈

40if(u == -1) break;41

42 color[u] =black;

43//

更新與之相關的頂點

44for(int v = 0; v < n; v++) 51}

52}53}

5455

int sum = 0;56

for(int i = 0; i < n; i++)

59return

sum;60}

6162

intmain() 71}

7273 cout << prim() <74return0;

75}

76/*775

78-1 2 3 1 -1

792 -1 -1 4 -1

803 -1 -1 1 1

811 4 1 -1 3

82-1 -1 1 3 -1

83*/

最小生成樹 次小生成樹

一 最小生成樹 說到生成樹首先要解釋一下樹,樹是乙個聯通的無向無環圖,多棵樹的集合則被稱為森林。因此,樹具有許多性質 1.兩點之間的路徑是唯一的。2.邊數等於點數減一。3.連線任意兩點都會生成乙個環。對於乙個無向聯通圖g的子圖,如果它包含g的所有點,則它被稱為g的生成樹,而各邊權和最小的生成樹則被稱...

最小生成樹

package 圖 最小生成樹是用最少的邊吧把所有的節點連線起來。於是和圖的深度優先搜素差不多。class stack public void push int key public int pop 檢視棧頂的元素 public int peek public boolean isempty cla...

最小生成樹

define max vertex num 20 最大頂點數 typedef int adjmatrix max vertex num max vertex num 鄰接矩陣型別 typedef char vertextype typedef struct mgraph struct dnodecl...