zoukankan      html  css  js  c++  java
  • hdu-1102 Constructing Roads(最小生成树)

    题目链接:

    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)   

     Memory Limit: 65536/32768 K (Java/Others)


    Problem 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
     
    题意:
     
    给出一个矩阵代表两个点之间的距离,现在要求把所有的点连起来,其中已经有一些点连起来了,问最少还要建多长的路才能把所有点连起来;
     
    思路:
     
    最小生成树的模板题;
     
    AC代码:
    /*1102    15MS    1684K    1365 B    G++    2014300227*/
    #include <bits/stdc++.h>
    using namespace std;
    const int N=103;
    
    typedef long long ll;
    const ll mod=1e9+7;
    const double PI=acos(-1.0);
    int n,a[103][103],p[103],q,u,v;
    int findset(int x)
    {
        if(x==p[x])return x;
        return p[x]=findset(p[x]);
    }
    void same(int x,int y)
    {
        int fx=findset(x),fy=findset(y);
        if(fx!=fy)p[fx]=p[fy];
    }
    struct Edge
    {
        int l,r,len;
    };
    Edge edge[5010];
    int cmp(Edge x,Edge y)
    {
        return x.len<y.len;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            int cnt=0;
            for(int i=1;i<=n;i++)
            {
                p[i]=i;
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&a[i][j]);
                    if(j>i)
                    {
                        edge[cnt].l=i;
                        edge[cnt].r=j;
                        edge[cnt++].len=a[i][j];
                    }
                }
            }
            sort(edge,edge+cnt,cmp);
            scanf("%d",&q);
            for(int i=1;i<=q;i++)
            {
                scanf("%d%d",&u,&v);
                same(u,v);
            }
            int ans=0;
            for(int i=0;i<cnt;i++)
            {
                if(findset(edge[i].l)!=findset(edge[i].r))
                {
                    ans+=edge[i].len;
                    same(edge[i].l,edge[i].r);
                }
            }
            printf("%d
    ",ans);
    
        }
    
        return 0;
    }
     
  • 相关阅读:
    如何基于 String 实现同步锁?
    Web前端开发必不可少的9个开源框架
    Java知识,面试总会问到虚拟机,虚拟机类加载机制你懂吗?
    带你了解Java的序列化与反序列化
    想自己写框架?不了解Java注解机制可不行
    深度解密:Java与线程的关系
    手把手教你分析Mysql死锁问题
    windows server 2012 安装 DockerToolbox
    .NET CORE MVC  返回 JSON 数据
    .net core ajax提交Controller接收不到的问题处理方法
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5386782.html
Copyright © 2011-2022 走看看