zoukankan      html  css  js  c++  java
  • 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 =)>

    自己火候不够 

     
  • 相关阅读:
    团队项目——站立会议DAY14
    团队项目——站立会议DAY13
    团队项目——站立会议DAY12
    团队项目——站立会议 DAY11
    团队项目——站立会议DAY10
    团队项目——站立会议DAY9
    团队项目——站立会议 DAY8
    团队项目——站立会议DAY7
    团队项目——站立会议DAY6
    作业五:团队项目——项目启动及需求分析
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4288951.html
Copyright © 2011-2022 走看看