zoukankan      html  css  js  c++  java
  • HDU 1863 畅通工程 (最小数prim)

    畅通工程
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。 

    Input

    测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 
    行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。 

    Output

    对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。 

    Sample Input

    3 3
    1 2 1
    1 3 2
    2 3 4
    1 3
    2 3 2
    0 100

    Sample Output

    3
    ?

     题意:给n条路,m个村子,接下来n行,每行3个数据(a b c),代表a,b村相连,成本为c,村子间都要直接或间接连起来。

     思路:依旧是prim只是多俩判断

                      1:路n>=m-1,否则村子不可能连通

                      2:vis标记的每一个村子都要被标记过一次

    AC代码如下:

     1 #include<cstdio>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 #define inf 0x3f3f3f3f
     6 int map[105][105];
     7 void prim(int m)
     8 {
     9     int vis[105], dis[105];
    10     int ans = 0;
    11     memset(vis, 0, sizeof(vis));//记录该点是否被访问过 
    12     memset(dis, inf, sizeof(dis)); 
    13     dis[1] = 0;//从第一个点开始至第n个点 
    14     for(int i = 1; i <= m; i++)
    15     {
    16         int k = -1, tp = inf;
    17         for(int j = 1; j <= m; j++)
    18             //寻找权最小的路 
    19             if(!vis[j] && dis[j] < tp)
    20             {
    21                 k = j;//最小权连通的点(还未标记的) 
    22                 tp = dis[j];//最小的权的大小(记为x) 
    23             }
    24         vis[k] = 1;//标记新的点 
    25         ans += tp;//加入新的权值 
    26         //printf("%d
    ", ans);
    27         for(int j = 1; j <= m; j++)
    28         {   //寻找新的最小权值 (仅次于x小的权值) 
    29             if(!vis[j] && map[k][j] < dis[j])
    30                 dis[j] = map[k][j];
    31         }
    32     }
    33     for(int j = 1; j <= m; j++)
    34         if(vis[j] == 0){
    35             printf("?
    ");
    36             return ;
    37         }
    38     printf("%d
    ", ans);
    39 }
    40 int main()
    41 {
    42     int n, m, a, b, c;
    43     //道路n条 村庄m个 
    44     while( scanf("%d %d", &n, &m), n)
    45     {   //初始化地图上的权,设为最大值 
    46         memset(map, inf, sizeof(map));
    47         for(int i = 0; i < n; i++){
    48             scanf("%d %d %d", &a, &b, &c);
    49             map[a][b] = map[b][a] = c;//无向图 
    50         }
    51         if(n<m-1){
    52             printf("?
    ");
    53             continue;
    54         }
    55         prim(m);                
    56     }
    57     return 0;
    58 }
     
  • 相关阅读:
    CNN comprehension
    Gradient Descent
    Various Optimization Algorithms For Training Neural Network
    gerrit workflow
    jenkins job配置脚本化
    Jenkins pipeline jobs隐式传参
    make words counter for image with the help of paddlehub model
    make words counter for image with the help of paddlehub model
    git push and gerrit code review
    image similarity
  • 原文地址:https://www.cnblogs.com/123tang/p/5746723.html
Copyright © 2011-2022 走看看