zoukankan      html  css  js  c++  java
  • 6.1.1 Constructing Roads

    Constructing Roads

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

    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

    思路:裸的最小生成树(prim)还没堆优化

     1 #include <cstdio>
     2 #include <cstring>   
     3 #include <iostream>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <cstdlib>
     7 #include <queue>
     8 using namespace std;
     9 
    10 const int maxn=110,INF=1000000000;
    11 int x,y,z,n,m,cnt,p,ans,pos,minn;
    12 bool f[maxn];
    13 int a[maxn][maxn],c[maxn];
    14 
    15 void close()
    16 {
    17     exit(0);
    18 }
    19 
    20 void prim()
    21 {
    22     memset(f,false,sizeof(f));
    23     f[1]=true;ans=0;
    24     for (int i=2;i<=n;i++)
    25         c[i]=a[i][1];
    26     for (int i=1;i<=n-1;i++)
    27     {
    28         minn=INF;
    29         for (int j=1;j<=n;j++)
    30             if (minn>c[j] && not f[j])
    31             {
    32                 minn=c[j];
    33                 pos=j;
    34             }
    35         ans+=minn;
    36         f[pos]=true;
    37         for (int j=1;j<=n;j++)
    38             if (j!=pos && not f[j] && a[pos][j]<c[j])
    39                 c[j]=a[pos][j];
    40     }
    41     printf("%d\n",ans);
    42 }
    43 
    44 void init()
    45 {
    46     while (scanf("%d",&n)!=EOF)
    47     {
    48         for (int i=1;i<=n;i++)
    49             for (int j=1;j<=n;j++)
    50             {
    51                 scanf("%d",&x);
    52                 a[i][j]=x;
    53             }
    54         scanf("%d",&m);
    55         for (int i=1;i<=m;i++)
    56         {
    57             scanf("%d %d",&x,&y);
    58             a[x][y]=0;a[y][x]=0;
    59         }
    60         prim();
    61     }
    62 }
    63 
    64 
    65 int main ()
    66 {
    67     init();
    68     close();
    69 }
  • 相关阅读:
    端口被占用
    启动Windows防火墙提示“0x8007042c"
    vue创建全局组件
    vue中过度动画之列表添加删除动画实现
    vue中过渡动画(类名结合动画实现方式)
    vue中过渡动画(类名实现方式)
    this.$nextTick()方法的使用
    利用axios获取数据并渲染到视图层
    axios的简单使用
    watch深度监听
  • 原文地址:https://www.cnblogs.com/cssystem/p/3045942.html
Copyright © 2011-2022 走看看