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

    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
    kicc
     

    Recommend
    Eddy

    最小生成树的模板题目

    下面的代码使用了Prim算法

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<map>
     7 #include<iomanip>
     8 #include<queue>
     9 #define INF 0x7ffffff
    10 #define MAXN 200
    11 using namespace std;
    12 const double eps=1e-10;
    13 const double PI=acos(-1);
    14 int G[MAXN][MAXN];
    15 int vnew[MAXN];
    16 int lowval[MAXN];
    17 int sum;
    18 int n,q;
    19 void Prim(int start)
    20 {
    21     int j,mi;
    22     for(int i=1;i<=n;i++){
    23         if(i!=start){
    24             lowval[i]=G[start][i];
    25             vnew[i]=0;
    26         }
    27     }
    28     vnew[start]=1;
    29     for(int i=1;i<=n-1;i++){
    30         j=-1;
    31         mi=INF;
    32         for(int i=1;i<=n;i++){
    33             if(vnew[i]==0&&lowval[i]<mi){
    34                 j=i;
    35                 mi=lowval[i];
    36             }
    37         }
    38             vnew[j]=1;
    39             sum+=lowval[j];
    40             for(int i=1;i<=n;i++){
    41                 if(vnew[i]==0){
    42                     lowval[i]=min(lowval[i],G[j][i]);
    43                 }
    44             }
    45     }
    46 }
    47 int main()
    48 {
    49     #ifndef ONLINE_JUDGE
    50     freopen("data.in", "r", stdin);
    51     #endif
    52     std::ios::sync_with_stdio(false);
    53     std::cin.tie(0);
    54     //Prim算法
    55     int a,b;
    56     while(cin>>n){
    57         for(int i=1;i<=n;i++){
    58             for(int j=1;j<=n;j++){
    59                 cin>>G[i][j];
    60             }
    61         }
    62         cin>>q;
    63         for(int i=0;i<q;i++){
    64             cin>>a>>b;
    65             G[a][b]=G[b][a]=0;
    66         }
    67         sum=0;
    68         Prim(1);
    69         cout<<sum<<endl;
    70     }
    71 
    72 }
  • 相关阅读:
    IoC之Ninject
    C#中的扩展方法
    Office 2016 Pro Plus Project 专业版 Visio 专业版 64 位vol版本方便KMS小马oem
    Microsoft Office 2016 简体中文 Vol 版镜像下载
    svn 被锁住 冲突 Can't revert without reverting children
    定时检查服务批处理,发现服务停止立即启动服务
    C++中关于[]静态数组和new分配的动态数组的区别分析
    IP地址与无符号整数值相互转换
    算法:整数与ip地址转换
    将字符串表示的IP地址转变为整形表示
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6234864.html
Copyright © 2011-2022 走看看