kruskal最小生成树

<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">(1) </span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><a target=_blank href="http://www.wutianqi.com/?p=1286" style="color: rgb(51, 102, 153); text-decoration: none;">克鲁斯卡尔算法</a></span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是任意的.该方法对于边相对比较多的不是很实用,浪费时间.</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">方法:将图中边按其权值由小到大的次序顺序选取,若选边后不形成回路,则保留作为一条边,若形成回路则除去.依次选够(n-1)条边,即得最小生成树.(n为顶点数)
<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/1.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1316" title="kruskal1" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/1.gif" width="113" height="113" style="border: none; max-width: 100%;" /></a>
第一步:由边集数组选第一条边
<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/2.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1317" title="kruskal2" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/2.gif" width="48" height="87" style="border: none; max-width: 100%;" /></a>
第二步:选第二条边,即权值为2的边
<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/3.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1318" title="3" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/3.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a>
第三步:选第三条边,即权值为3的边
<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/4.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1319" title="4" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/4.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a>
第四步:选第四条边,即权值为4的边
<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/5.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1320" title="5" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/5.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a>
第五步:选第五条边
<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/6.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1321" title="6" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/6.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p>
int r[maxn],p[maxn],u[maxn],v[maxn],w[maxn];
int n,m,ans=0;
bool cmp(const int i, const int j){return w[i]<w[j];}
int find(int x){return p[x]==x?x:find(p[x]);}//并查集
int krus(){
    for(int i=1;i<=n;i++)p[i]=i;//初始化并查集
    for(int i=1;i<=m;i++)r[i]=i;//初始化边
    sort(r+1,r+m+1,cmp);
    for(int i=1;i<=m;i++){
    int e=r[i]; int x=find(u[e]);int y=find(v[e]);
    if(x!=y){
            printf("%d-----%d\n",x,y);//打印连线的两个点
            ans+=w[e];
            p[x]=y;//加入
        }
    }
        return ans;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值