• Constructing Roads (MST)


    Constructing Roads
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum. 
     

    Input

    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j. 

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built. 
     

    Output

    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. 
     

    Sample Input

    3 0 990 692 990 0 179 692 179 0 1 1 2
     

    Sample Output

    179
     
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    #define S 110
    struct edge
    {
        int u , v , w ;
    }e[S * S];
    int n , m ,p[S];//这一这三个变量的位置要小心,不然,嘿嘿
    
    void init ()
    {
        for(int i = 1 ; i <= S ; i++)
            p[i] = i ;
    }
    
    bool cmp (edge a , edge b)
    {
        return a.w < b.w ;
    }
    
    int find (int x)
    {
        return x == p[x] ? x : p[x] = find(p[x]) ;
    }
    
    void kruskal ()
    {
        sort (e + 1 , e + m , cmp) ;
        int ans = 0 , x , y ;
        for (int i = 1 ; i < m ; i++) {
            x = find (e[i].u) ;
            y = find (e[i].v) ;
            if (x != y) {
                ans += e[i].w ;
                p[x] = y ;
            }
        }
        printf ("%d
    " , ans) ;
    }
    int main ()
    {
       // freopen ("a.txt" , "r" ,stdin) ;
        int k , built ;
        int x , y ;
        while (~ scanf ("%d" , &n ) ) {
            init () ;
            m = 1 ;
            for (int i = 1 ; i <= n ; i++) {
                for (int j = 1 ; j <= n ; j++) {
                    if (j <= i) {
                        scanf ("%d" , &k ) ;
                        continue ;
                    }
                    scanf ("%d" , &e[m].w) ;
                    e[m].u = i ;
                    e[m].v = j ;
                    m++ ;
                }
            }
            scanf ("%d" , &built );
            for (int i = 0 ; i < built ; i++) {
                scanf ("%d%d" , &x , &y ) ;
                x = find (x) ;
                y = find (y) ;
                p[x] = y ;
            }
            kruskal () ;
        }
        return 0 ;
    }

    以前碰到“以建”这种问题,我就只会想到把w = 0 ;

    但没想到还能把“以建”的两顶点,先串起来的方法,充分利用了kruskal的性质,不赞不行 <(= I =)>

    自己火候不够 

     
  • 相关阅读:
    VC++ 利用PDB和dump文件定位问题并进行调试
    MFC限制edit控件的字符输入长度
    VC++ 使用CreateProcess创建新进程
    正则表达式验证HTTP地址是否合法
    C++ _access和_waccess的使用方法
    最后一次谈 VirtualBox的安装方法
    解决/var/log下没有messages文件的问题?
    待续未完- 自己写后台内容管理程序的辅助内容
    php中的正则函数:正则匹配,正则替换,正则分割 所有的操作都不会影响原来的字符串.
    未完待续
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4288951.html
Copyright © 2020-2023  润新知