zoukankan      html  css  js  c++  java
  • HDU 1102

    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10151    Accepted Submission(s): 3782

    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
     
    Source
     
    Recommend
    Eddy

    算法:Prim算法

    代码:
    //prim算法
    #include<iostream>
    #include<cstring>
    using namespace std;
    int map[110][110];
    int dis[110];    //用于存储未在集合内的点到已在集合内的点的最短距离,
    //即下标表示结点,值表示到集合内点的最短距离,通过visited[]标注是否已经在集合内
    int visited[110];//visited[index]值为1,表明在集合内,为0,不在集合内
    void prim(int n)
    {
        int i,j,now,sum;//now表示最新进入集合的点的下标
        memset(visited,0,sizeof(visited));
        for(i=1;i<=n;i++)
        {
            dis[i]=map[1][i];
        }
        visited[1]=1;
        sum=0;
        for(i=1;i<n;i++)
        {
            int min=1010;
            //寻找最短距离
            for(j=1;j<=n;j++)
            {
                if(visited[j]==0 && dis[j]<min)
                {
                    min=dis[j];
                    now=j;
                }
            }
            sum+=min;
            visited[now]=1;
            //更新未在集合内的点到已在集合点的最短距离
            for(j=1;j<=n;j++)
            {
                if(visited[j]==0 && dis[j]>map[now][j])
                {
                    dis[j]=map[now][j];
                }
            }
        }
        cout<<sum<<endl;
    }
    int main()
    {
        int n,i,j,q,a,b;
        while(cin>>n)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    cin>>map[i][j];
                }
            }
            cin>>q;
            while(q--)
            {
                cin>>a>>b;
                map[a][b]=map[b][a]=0;//已经连上的,距离设为0
            }
            prim(n);
        }
        return 0;
    }
    


  • 相关阅读:
    crs_stop 错误一列
    本地管理表空间和字典管理表空间的特点,ASSM有什么特点
    RAC 11.2的新特性
    oracle 11g RAC 的一些基本概念(三)
    【1】【leetcode-130】 被围绕的区域
    【leetcode-125】 验证回文串
    【1】【leetcode-127】单词接龙word-ladder
    【1】【leetcode-115】 不同的子序列 distinct-subsequences
    【1】[leetcode-124] 二叉树中的最大路径和
    [leetcode-128] 最长连续序列
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3073262.html
Copyright © 2011-2022 走看看