kruskal

kruskal 时间复杂度 O(|E| * log|V|)

按权值顺序依次判断,如果不产生圈则放进去

时间主要用在边的排序

代码来自《挑战编程》

 1 const int MAX_E = 100;
 2 const int MAX_V = 30;
 3
 4 struct edge {
 5     int u, v, cost;
 6 };
 7 bool comp(const edge &a, const edge &b) {
 8     return a.cost < b.cost;
 9 }
10 edge es[MAX_E];
11 int V, E;
12 void add(int _u, int _v, int _cost) {
13     es[E].u = _u;
14     es[E].v = _v;
15     es[E].cost = _cost;
16     E++;
17 }
18 //并查集 判断联通性
19 //***
20 int par[MAX_V];
21 int ran[MAX_V];
22 void init() {
23     for(int i = 0; i < V; i++) par[i] = i, ran[i] = 0;
24 }
25 int finds(int x) {
26     if(par[x] == x) return x;
27     return par[x] = finds(par[x]);
28 }
29 void unite(int x, int y) {
30     x = finds(x), y = finds(y);
31     if(x == y) return;
32     if(ran[x] < ran[y]) par[x] = y;
33     else {
34         par[y] = x;
35         if(ran[x] == ran[y]) ran[x]++;
36     }
37
38 }
39 bool same(int x, int y) {
40     return finds(x) == finds(y);
41 }
42 //***
43
44 //kruskal 最小生成树
45 //***
46 int kruskal() {
47     sort(es, es+E, comp);
48     init();
49     int res = 0;
50     for(int i = 0; i < E; i++) {
51         edge e = es[i];
52         if(!same(e.u, e.v)) {
53             unite(e.u, e.v);
54             //printf("%d %d %d\n", e.u, e.v, e.cost);
55             res += e.cost;
56         }
57     }
58     return res;
59 }
60 //***
时间: 2024-08-26 06:00:03

kruskal的相关文章

【BZOJ 3551】[ONTAK2010] Peaks加强版 Kruskal重构树+树上倍增+主席树

这题真刺激...... I.关于Kruskal重构树,我只能开门了,不过补充一下那玩意还是一棵满二叉树.(看一下内容之前请先进门坐一坐) II.原来只是用树上倍增求Lca,但其实树上倍增是一种方法,Lca只是他的一种应用,他可以搞各种树上问题,树上倍增一般都会用到f数组. |||.我们跑出来dfs序就能在他的上面进行主席树了. IV.别忘了离散. V.他可能不连通,我一开始想到了,但是我觉得出题人可能会是好(S)人(B),但是...... #include <cstdio> #include

最小生成树求法 Prim + Kruskal

prim算法的思路 和dijkstra是一样的 每次选取一个最近的点 然后去向新的节点扩张 注意这里的扩张 不再是 以前求最短路时候的到新的节点的最短距离 而是因为要生成一棵树 所以是要连一根最短的连枝 所以关键部分修改一下 dist[u] = min(dist[u], e.cost) --->>e是连接 v 和 u的边 同样地 普同写法O(v^2) 用队列优化后O(E*logV) 1 #include <iostream> 2 #include <stdio.h> 3

HDU 1389 继续畅通工程【最小生成树,Prime算法+Kruskal算法】

继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21871    Accepted Submission(s): 9356 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列

poj1861 最小生成树 prim &amp; kruskal

// poj1861 最小生成树 prim & kruskal // // 一个水题,为的只是回味一下模板,日后好有个照应不是 #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <iostream> using namespace std; const int MAX_N = 1008; const int INF =

hdu 1875 畅通工程再续(kruskal算法计算最小生成树)

畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18411    Accepted Submission(s): 5769 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先

HDU2122 Ice_cream’s world III【Kruskal】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 997    Accepted Submission(s): 321 Problem Description ice_cream's world becomes stronger and stronger; every road is built

最小生成树 Prim(普里姆)算法和Kruskal(克鲁斯特尔)算法

Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且其所有边的权值之和亦为最小.该算法于1930年由捷克数学家沃伊捷赫·亚尔尼克(英语:Vojtěch Jarník)发现:并在1957年由美国计算机科学家罗伯特·普里姆(英语:Robert C. Prim)独立发现:1959年,艾兹格·迪科斯彻再次发现了该算法.因此,在某些场

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e