zoukankan      html  css  js  c++  java
  • HDOJ1102 Constructing Roads[Prim()]

    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7661    Accepted Submission(s): 2861


    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

    无压力水过

    code:

     1 #include <iostream>   
     2 #include <iomanip>   
     3 #include <fstream>   
     4 #include <sstream>   
     5 #include <algorithm>   
     6 #include <string>   
     7 #include <set>   
     8 #include <utility>   
     9 #include <queue>   
    10 #include <stack>   
    11 #include <list>   
    12 #include <vector>   
    13 #include <cstdio>   
    14 #include <cstdlib>   
    15 #include <cstring>   
    16 #include <cmath>   
    17 #include <ctime>   
    18 #include <ctype.h> 
    19 using namespace std;
    20 
    21 #define MAXN 102
    22 
    23 int map[MAXN][MAXN];
    24 int vst[MAXN];
    25 int dis[MAXN];
    26 int n;
    27 
    28 int prim()
    29 {
    30     int i,j,k;
    31     int sum=0;
    32     memset(vst,0,sizeof(vst));
    33     for(i=1;i<=n;i++)
    34         dis[i]=map[1][i];
    35     dis[1]=0;
    36     vst[1]=1;
    37     for(i=2;i<=n;i++)
    38     {
    39         k=1;
    40         int temp=9999999;
    41         for(j=1;j<=n;j++)
    42             if(dis[j]<temp&&!vst[j])
    43             {
    44                 temp=dis[j];
    45                 k=j;
    46             }
    47         sum+=temp;
    48         vst[k]=1;
    49         for(j=1;j<=n;j++)
    50         {
    51             if(map[k][j]<dis[j])
    52                 dis[j]=map[k][j];
    53         }
    54     }
    55     return sum;
    56 }
    57 
    58 int main()
    59 {
    60     int i,j;
    61     int a,b;
    62     while(~scanf("%d",&n))
    63     {
    64         for(i=1;i<=n;i++)
    65             for(j=1;j<=n;j++)
    66                 scanf("%d",&map[i][j]);
    67         int t;
    68         scanf("%d",&t);
    69         while(t--)
    70         {
    71             scanf("%d%d",&a,&b);
    72             map[a][b]=map[b][a]=0;
    73         }
    74         printf("%d\n",prim());
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    从汇编看c++中的placement operator new
    从汇编看c++的new和delete
    从汇编看c++中全局对象和全局变量
    javascript中的this
    好工具
    js压缩解压工具
    IE的documentMode属性
    77. sqlserver 锁表解决方式
    75. ID重新走过,备份表
    5. Java中序列化的serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/XBWer/p/2633190.html
Copyright © 2011-2022 走看看