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 =)>

    自己火候不够 

     
  • 相关阅读:
    如何让ListView的item不可点击
    [Android] ListView中如何让onClick和onItemClick事件共存
    [Android] RelativeLayout, LinearLayout,FrameLayout
    [Android]drawable-nodpi文件夹 的作用
    [转]安装和使用JD-Eclipse插件
    jmeter下载及安装配置
    MySql安装后在服务管理器里边找不到MySql服务项的解决办法
    两步破解IntelliJ IDEA 教程(无敌版)
    进程和线程区别和联系
    webservice--cxf和spring结合,发布restFull风格的服务
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4288951.html
Copyright © 2011-2022 走看看