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)
    Total Submission(s): 24129    Accepted Submission(s): 9269

    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   |   We have carefully selected several similar problems for you:  1301 1162 1272 1856 1325 
     

    题意就是要修路,给出代表城市数目,又给出城市的邻接矩阵,有些城市之间已经有道路了,这些道路连接的两个城市之间的距离直接置零就可以了,然后就是求最小生成树,可以用prim算法或者kruskal算法,因为给出的是邻接矩阵,所以用prim算法比较好,用kruskal算法可以提前将有道路的两个顶点合并就可以了,prim算法适用于稠密图O(N^2),kruskal适合稀疏图O(eloge).

    代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    const int N = 1000 + 5;
    const int INF = 0x3f3f3f3f;
    int mat[N][N],lowc[N];
    bool visit[N];
    
    int prim(int n){
        int i,k,cnt,ret=0;
        memset(lowc,INF,sizeof(lowc));
        memset(visit,0,sizeof(visit));
        for(lowc[1]=cnt=0;cnt < n;cnt++){
            for(k=-1,i=1;i<=n;i++)
                if(!visit[i]&&(k==-1||lowc[i] < lowc[k]))
                k = i;
            for(visit[k]=true,ret+=lowc[k],i=1;i<=n;i++)
                if(!visit[i]&&mat[k][i] < lowc[i])
                lowc[i] = mat[k][i];
        }
        return ret;
    }
    int main(){
        int n,m,u,v;
        while(scanf("%d",&n)==1){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++) scanf("%d",&mat[i][j]);
            scanf("%d",&m);
            for(int i=1;i<=m;i++){
                scanf("%d %d",&u,&v);
                mat[u][v] = mat[v][u] = 0;
            }
            printf("%d
    ",prim(n));
        }
    }
     



  • 相关阅读:
    Spine(2D骨骼动画)
    UpdatePanel的用法
    Windows7下的免费虚拟机(微软官方虚拟机)
    android ViewPager具体解释
    与Scheme共舞
    Binder机制1---Binder原理介绍
    HDU 2853 Assignment(KM最大匹配好题)
    微信公众平台java开发具体解释(project代码+解析)
    怎样绕过oracle listener 监听的password设置
    IOS成长之路-Nsstring中搜索方法rangeOfString
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7384047.html
Copyright © 2011-2022 走看看