最小生成树之Kruskal

思路:(贪心)

排序边的权值,按从小到大排序,然后从最小权值开始,一直连接点(把他们的父亲变成同一个),最后连成的树就是最小生成树

代码实现(hdu 1233)

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int v[100006];
struct node
{
    int x,y,len;
}road[10006];
int f(int a)
{
    return a==v[a]?a:f(v[a]);
}
bool cmp(node a,node b)
{
    return a.len<b.len?1:0;
}
int main()
{

    int n,ans=0;
    while(scanf("%d",&n),n)
    {
        for(int i=1;i<=n;i++)
    	v[i]=i;
    	ans=0;
        int ans = 0;
        int t = n*(n-1)/2;
        for(int i=0;i<t;i++)
        {
            scanf("%d %d %d",&road[i].x,&road[i].y,&road[i].len);
        }
        sort(road,road+t,cmp);
        for(int i=0;i<t;i++)
        {
        	//cout<<n<<endl;
            int fa = f(road[i].x);
            int fb = f(road[i].y);
            if(fa!=fb)
            {
                ans+=road[i].len;
                v[fa] = fb;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-01 22:47:16

最小生成树之Kruskal的相关文章

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 =

最小生成树之Kruskal算法

上一篇文章中提到了最小生成树的Prim算法,这一节继续探讨一下最小生成树的Kruskal算法.什么是最小生成树算法上文已经交代过了,所以我们直接从Kruskal的步骤开始介绍. 1.Kruskal算法的步骤: a.假定拓扑图的边的集合是E,初始化最小生成树边集合G={}. b. 遍历集合E中的所有元素,并且按照权值的大小进行排序. c. 找出E中权值最小的边e . d .如果边e不和最小生成树集合G中的边构成环路,则将边e加到边集合G中:否则测试下一条权值次小的边,直到满足条件为止. e. 重复

最小生成树 prime kruskal

带权图分为有向和无向 无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法: 有向图的最短路径算法,有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树 生成树是联通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则 将出现一个回路:若去掉一条边,将会使之编程非连通图.生成树各边的权 值总和称为生成素的权.权最小的生成树称为最小生成树,常用的算法有prime算法和kruskal算法. 最小生成树

ZOJ 1203 Swordfish 剑鱼行动 最小生成树,Kruskal算法

题目链接:ZOJ 1203 Swordfish 剑鱼行动 Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, passwords and the most advanced security systems.

最小生成树(kruskal模版 Prim模板)

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 最小生成树,最重要的是了解思想 稠密图用Prim,稀疏图用Kruskal K(每次找最小的边连接,一条边连接两个点,所以单路就可以了) 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 int bin[110]; 5 struct node 6 { 7 int

poj——2031 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio

HDU 1863 (最小生成树之Kruskal) 畅通工程

模板题,学习一下最小生成树的Kruskal算法 对于稀疏图来说 按所给的边的权值从小到大排序,如果该边不与已经选的边形成环就选择它 这里用并查集来实现 第i条边的端点放在u.v数组中,权值保存在w中 这里用的是间接排序,也就是排的是每条边的序号,放在rank数组中 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std;

ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, password

数据结构(C实现)------- 最小生成树之Kruskal算法

[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020] 算法描述: Kruskal算法是按权值递增的次序来构造最小生成树的方法. 假设G(V,E)最一个具有n个顶点的连通网,顶点集V={v1,v2,....,vn}.设所求的最小生成树为T={U,TE},其中U是T的顶点集,TE是T的边集,U和TE的初始值为空集.Kruskal算法的基本思想如下:将最小生成树初始化为T=(V,TE),仅包含 G的全部顶点,不包含G的任一条边,此时

ACM:最小生成树,kruskal &amp;&amp; prim,并查集

题目: 输入顶点数目,边的数目,输入每条边的两个顶点编号还有每条边的权值,求最小生成树,输出最小生成树的权值.. 注意:prim算法适合稠密图,其时间复杂度为O(n^2),其时间复杂度与边得数目无关,而kruskal算法的时间复杂度为O(eloge)跟边的数目有关,适合稀疏图. kruskal----归并边:prim----归并点 方法一:kruskal,克鲁斯卡尔,并查集实现. #include <iostream> #include <algorithm> using name