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

    题目:

    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

    题意:
    给出n个点 求最小生成树 其中已经连上了一些边 求需要连上的边的总长

    思路:
    将已经连上的边的权值标记为0 用prim算法跑最小生成树

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int inf=0x3f3f3f3f;
    const int maxn=110;
    int n,tmp,m,l,r;
    int mp[maxn][maxn],dis[maxn],vis[maxn];
    
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            dis[i]=inf;
            for(int j=1;j<=n;j++){
                scanf("%d",&mp[i][j]);
            }
        }
        int sum=0;
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&l,&r);
            mp[l][r]=0;
            mp[r][l]=0;
        //    vis[l]=1;vis[r]=1;
        //    sum+=mp[l][r];
        }
        for(int i=1;i<=n;i++){
            dis[i]=mp[1][i];
        }
        dis[1]=0;
        vis[1]=1;
        for(int i=1;i<=n;i++){
            tmp=inf;
            int minn=inf;
            for(int j=1;j<=n;j++){
                if(vis[j]==0 && dis[j]<minn){
                    tmp=j;
                    minn=dis[j];
                }
            }
            if(tmp==inf) break;
            vis[tmp]=1;
            sum+=minn;
            for(int j=1;j<=n;j++){
                if(vis[j]==0 && dis[j]>mp[tmp][j]){
                    dis[j]=mp[tmp][j];
                }
            }
        }
        printf("%d
    ",sum);
        return 0;
    }
  • 相关阅读:
    jsp大文件(视频)上传问题
    python多版本的pip共存问题解决办法
    buntu系统安装rpm包的方法
    如何查看linux是32位还是64位
    python的if else
    python获取列表唯一值
    python列表转数组
    python文件操作write与writelines的区别
    python中字符串str的strip()方法
    ubuntu16.04安装cloudcompare及打开方法
  • 原文地址:https://www.cnblogs.com/whdsunny/p/10528754.html
Copyright © 2011-2022 走看看