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;
    }
  • 相关阅读:
    判断数据库内容,在页面显示自定义数据case when
    其他窗体赋值给comboBox实现值的回显,并使赋的值处于选中状态(根据text获取selectedindex)
    DataGridView的按钮列的点击事件
    表单中各种input汇总
    块元素、内敛元素、行内块元素特点、相互转换、取消默认效果
    内联元素,取消间隙和默认效果
    margin-top塌陷
    linux下安装 配置 redis数据库
    redis数据库各种数据类型的增删改查命令
    python与mongodb的交互 增删改差
  • 原文地址:https://www.cnblogs.com/whdsunny/p/10528754.html
Copyright © 2011-2022 走看看