zoukankan      html  css  js  c++  java
  • POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Appoint description: 

    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 <iostream>
    #include <cstdio>
    #include <string>
    #include <queue>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <cstring>
    #include <cctype>
    #include <cstdlib>
    #include <cmath>
    #include <ctime>
    using    namespace    std;
    
    const    int    SIZE = 105;
    int    N,M,NUM;
    int    FATHER[SIZE];
    struct    Node
    {
        int    from,to,cost;
    }G[SIZE * SIZE];
    
    void    ini(void);
    int    find_father(int);
    void    unite(int,int);
    bool    same(int,int);
    bool    comp(const Node &,const Node &);
    int    kruskal(void);
    int    main(void)
    {
        int    from,to,cost;
    
        while(scanf("%d",&N) != EOF)
        {
            ini();
            for(int i = 1;i <= N;i ++)
                for(int j = 1;j <= N;j ++)
                {
                    scanf("%d",&cost);
                    if(i == j)
                        continue;
                    if(i < j)
                    {
                        G[NUM].from = i;
                        G[NUM].to = j;
                        G[NUM].cost = cost;
                        NUM ++;
                    }
                }
            scanf("%d",&M);
            while(M --)
            {
                scanf("%d%d",&from,&to);
                unite(from,to);
            }
            sort(G,G + NUM,comp);
            printf("%d
    ",kruskal());
        }
    
        return    0;
    }
    
    void    ini(void)
    {
        NUM = 0;
        for(int i = 1;i <= N;i ++)
            FATHER[i] = i;
    }
    
    int    find_father(int n)
    {
        if(n == FATHER[n])
            return    n;
        return    FATHER[n] = find_father(FATHER[n]);
    }
    
    void    unite(int x,int y)
    {
        x = find_father(x);
        y = find_father(y);
    
        if(x == y)
            return    ;
        FATHER[x] = y;
    }
    
    bool    same(int x,int y)
    {
        return    find_father(x) == find_father(y);
    }
    
    bool    comp(const Node & a,const Node & b)
    {
        return    a.cost < b.cost;
    }
    
    int    kruskal(void)
    {
        int    ans = 0,count = 0;
    
        for(int i = 0;i < NUM;i ++)
            if(!same(G[i].from,G[i].to))
            {
                unite(G[i].from,G[i].to);
                ans += G[i].cost;
                count ++;
                if(count == N - 1)
                    break;
            }
        return    ans;
    }


  • 相关阅读:
    sqlalchemy
    tornado-模板继承extend,函数和类的导入
    vi规范
    Spark 分布式SQL引擎
    Spark SQL 编程
    Spark SQL 基本原理
    spark SQL概述
    spark 多语言编程
    hadoop YARN
    spark 存储管理机制
  • 原文地址:https://www.cnblogs.com/xz816111/p/4547669.html
Copyright © 2011-2022 走看看