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 }
  • 相关阅读:
    EF – 4.CRUD与事务
    EF – 3.EF数据查询基础(下)数据关联
    EF – 2.EF数据查询基础(上)查询数据的实用编程技巧
    响应式的入门学习
    淘宝相关模块管理
    Git相关
    Drupal相关网站推荐
    在线视频播放软件
    Linux命令的常用
    Linux下使用Git命令及Github项目
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6234864.html
Copyright © 2011-2022 走看看