算法总结之最小生成树

最小生成树的作用:

有很多点,点点之间有很多边,边有边权,我们要选择一些边,将所有点互相联通,构成一颗树,即为最小生成树

模板题:http://codevs.cn/problem/1231/

算法:

prim

基本流程:开始从所有点 中任取一点,(通常取1号),然后找其他所有点离已经取得点构成的连通块最近的点,将其加入连通块,并将总代价加入这个点与联通块相连的边的边权,如此反复点数-1次即可。(找出离连通块最近的点可以使用堆优化)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using  namespace  std;
struct  lx
{
     long  long  dis;
     int  x;
};
const  bool   operator < ( const  lx x, const  lx y)
{
     return  x.dis > y.dis;
}
priority_queue <lx> dl;
int  head[110000],nxt[220000],to[220000],n,m;
int  qd,a,b,s,sum;
long  long  tot,w[220000];
bool  ins[110000];
void  cr( int  x, int  y, int  s)
{
     sum++;
     nxt[sum] = head[x];
     head[x] = sum;
     to[sum] = y;
     w[sum] = s;
}
int  main()
{
     scanf ( "%d%d" ,&n,&m);
     for  ( int  i = 1;i <= m;i++)
     {
         scanf ( "%d%d%d" ,&a,&b,&s);
         cr(a,b,s);
         cr(b,a,s);
     }
     ins[1] =  true ;
     qd = 1;
     for  ( int  i = 1;i < n;i++)
     {
         long  long  minn = 999999999999999;
         for  ( int  tp = head[qd];tp;tp = nxt[tp])
         {
             lx ttp;
             ttp.x = to[tp];
             ttp.dis = w[tp];
             dl.push(ttp);
         }
         bool  pc =  true ;
         while  (pc)
         {
             lx ptp = dl.top();
             dl.pop();
             if  (!ins[ptp.x])
             {
                 qd = ptp.x;
                 pc =  false ;
                 tot += ptp.dis;
                 ins[ptp.x] =  true ;
             }
         }
     }
     printf ( "%lld" ,tot);
     return  0;
}
kruskal

基本流程:

把所有边按照边权从小到大排序,从小到大扫,如当前边的两个端点,不在同一个并查集集合中,就将他们加入同一个并查集集合,并加入这条边。若两点在同一集合则忽略这条边。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using  namespace  std;
int  n,m,fa[110000],siz[110000];
long  long  tot;
struct  xn
{
     int  x,y,w;
} sz[110000];
const  bool  operator <( const  xn &a, const  xn &b)
{
     return  a.w < b.w;
}
int  getfa( int  x)
{
     if  (fa[x] == x)  return  x;
     return  fa[x] = getfa(fa[x]);
}
void  merge( int  x, int  y)
{
     int  fa1 = getfa(x),fa2 = getfa(y);
     if  (siz[fa1] < siz[fa2])
     {
         fa[fa1] = fa2;
         siz[fa2] += siz[fa1];
     } else
     {
         fa[fa2] = fa1;
         siz[fa1] += siz[fa2];
     }
}
int  main()
{
     scanf ( "%d%d" ,&n,&m);
     for  ( int  i = 1;i <= n;i++)
     {
         fa[i] = i;
         siz[i] =1;
     }
     for  ( int  i = 1;i <= m;i++)
     {
         scanf ( "%d%d%d" ,&sz[i].x,&sz[i].y,&sz[i].w);
     }
     sort(sz+1,sz+m+1);
     for  ( int  i = 1;i <= m;i++)
     {
         if  (getfa(sz[i].x) != getfa(sz[i].y))
         {
             merge(sz[i].x,sz[i].y);
             tot += ( long  long )sz[i].w;
         }
     }
     printf ( "%lld\n" ,tot);
     return  0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值