zoukankan      html  css  js  c++  java
  • 图论_最小生成树(Kruskal)

    在一个无向连通图中,如果存在一个连通子图包含原图中所有的结点和部分边,且这个子图不存在回路,那么我们称这个子图为原图的一棵生成树。在带权图中,所有的生成树中边权的和最小的那棵(或几棵)被称为最小生成树。

    最小生成树Kruskal算法的算法原理,它按照如下步骤求解最小生成树:

    (1)初始时所有结点属于孤立的集合。

    (2)按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为联通这两个集合的边中权值最小的那条)则确定该边为最小生成树上的一条边,并将这两个顶点分属的集合合并。

    (3)遍历完所有边后,原图上所有结点属于同一个集合则被选取的边和原图中所有结点构成最小生成树;否则原图不连通,最小生成树不存在。

     

    例 5.3 还是畅通工程 (1017)

    题目描述:某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
    输入:测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。当N为0时,输入结束,该用例不被处理。
    输出:对每个测试用例,在1行里输出最小的公路总长度。
    样例输入:
    3
    1 2 1
    1 3 2
    2 3 4
    4
    1 2 1
    1 3 4
    1 4 1
    2 3 3
    2 4 2
    3 4 5
    0
    样例输出:
    3
    5
    
    #include<cstdio>
    #include<algorithm>
    #define N 101
    using namespace std;
    int Tree[N];
    int findRoot(int x){
        if(Tree[x]==-1) return x;
        else{
            int tmp=findRoot(Tree[x]);
            Tree[x]=tmp;
            return tmp;
        }
    }
    struct E{
        int a,b;
        int cost;
        bool operator < (const E &A) const{
            return cost<A.cost;
        }
    }edge[6000];
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF&&n!=0){
            for(int i=1;i<=n*(n-1)/2;i++)
                scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].cost);
            sort(edge+1,edge+1+n*(n-1)/2);
            for(int i=1;i<=n;i++)
                Tree[i]=-1;
            int ans=0;
            for(int i=1;i<=n*(n-1)/2;i++){
                int a=findRoot(edge[i].a);
                int b=findRoot(edge[i].b);
                if(a!=b){
                    Tree[a]=b;
                    ans+=edge[i].cost;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }

     

    例 5.4 Freckles (1144)

    题目描述:In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad’s back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley’s engagement falls through. 

    Consider Dick’s back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

    输入:The first line contains 0 < n <= 100, the number of freckles on Dick’s back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

    输出:Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

    样例输入:
    3
    1.0 1.0
    2.0 2.0
    2.0 4.0
    1
    2
    3
    4
    5
    样例输出:
    3.41
    
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int Tree[101];
    struct Edge{
        int a;
        int b;
        double c;
        bool operator <(const Edge &A) const{
            return c<A.c;
        }
    }edge[6000];
    struct Point{
        double x;
        double y;
        double distance(Point A){
            return pow((x-A.x)*(x-A.x)+(y-A.y)*(y-A.y),0.5);
        }
    }list[101];
    int getRoot(int x){
        if(Tree[x]==-1) return x;
        else{
            int tmp=getRoot(Tree[x]);
            Tree[x]=tmp;
            return tmp;
        }
    }
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            for(int i=1;i<=n;i++){
                scanf("%lf%lf",&list[i].x,&list[i].y);
            }
            int t=1;
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    edge[t].a=i;
                    edge[t].b=j;
                    edge[t].c=list[i].distance(list[j]);
                    t++;
                }
            }
            sort(edge+1,edge+1+n*(n-1)/2);
            for(int i=1;i<=n;i++) Tree[i]=-1;
            double tot=0.0;
            for(int i=1;i<=n*(n-1)/2;i++){
                int a=getRoot(edge[i].a);
                int b=getRoot(edge[i].b);
                if(a!=b){
                    Tree[a]=b;
                    tot+=edge[i].c;
                }
            }
            printf("%.2f
    ",tot);
        }
        return 0;
    }
  • 相关阅读:
    安全传输平台项目扩展——keymngserver重构-硬件扩展
    安全传输平台项目扩展——C复习-C++复习-keymngclient重构
    安全传输平台项目——客户端代码移植-项目模块总结
    安全传输平台项目——配置管理终端-读写数据库
    根号分治刷题记录
    使用netsh命令来管理IP安全策略
    关于make_shared无法访问非公有构造函数的解决方法
    两两交换链表中的节点-递归解法
    Spring 的 AOP 简介
    Spring IOC和DI 注解开发
  • 原文地址:https://www.cnblogs.com/exciting/p/8639384.html
Copyright © 2011-2022 走看看