kruskal最小生成樹

2021-07-30 10:09:48 字數 721 閱讀 6293

kruskal 演算法是根據邊來進行一條一條地往生成樹中新增的,然後只有邊的兩點屬於不同集合的邊才能加入生成樹中;

想過用vis陣列標記然後進行看是否都用過了,如果都用過了,就不加入生成樹中;後面想想是我想錯了,如果出現兩個最小樹的情況,如果不把這兩個樹連在一起的話就不是最小生成樹了;

#include#include#include#include#include#includeusing namespace std;

typedef long long ll;

const ll maxn = 1000 + 10;

int n,m;

struct node

a[maxn];

bool cmp(const node &x,const node &y)

int fa[maxn];

void init()

int finds(int x)

int main()

init();

sort(a+1,a+n+1,cmp);

int ans = 0;

int cnt = 0;

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

if(cnt == m - 1)

}if(cnt != m - 1)

cout << "?" << endl;

} return 0;

}

kruskal 最小生成樹

include include 產生隨機數組用 include 同上 include using namespace std 1 帶權邊的類myarc class myarc bool operator const myarc arc myarc myarc int beginvex,int end...

最小生成樹Kruskal

最小生成樹有兩個特點,乙個是保證了所有邊的和是最小值,另乙個是保證了所有邊中的最大值最小。struct edge bool friend operator edge a,edge b 構邊 vectoredge int id max int mini void initial void input ...

最小生成樹(kruskal)

kruskal演算法 1 記graph中有v個頂點,e個邊 2 新建圖graphnew,graphnew中擁有原圖中相同的e個頂點,但沒有邊 3 將原圖graph中所有e個邊按權值從小到大排序 4 迴圈 從權值最小的邊開始遍歷每條邊 直至圖graph中所有的節點都在同乙個連通分量中 if 這條邊連線...