zoukankan      html  css  js  c++  java
  • HDU 1233 还是畅通工程

    还是畅通工程

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 42604    Accepted Submission(s): 19408

    Problem Description

    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

    Input

    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
    当N为0时,输入结束,该用例不被处理。

    Output

    对每个测试用例,在1行里输出最小的公路总长度。 

    Sample Input

    3

    1 2 1

    1 3 2

    2 3 4

    4

    1 2 1

    1 3 4

    1 4 1

    2 3 3

    2 4 2

    3 4 5

    0

    Sample Output

    3

    5

    Hint

    Hint

    Huge input, scanf is recommended.

    Source

    浙大计算机研究生复试上机考试-2006年  

     

    Recommend

    JGShining   |   We have carefully selected several similar problems for you:  1232 1102 1875 1879 1301 

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdio>
     5 using namespace std;
     6 const int MAX_N=10010;
     7 int n,tot,fa[MAX_N];
     8 long long MST;
     9 
    10 struct node{
    11     int from,to,value;
    12     bool operator < (const node &a) const{
    13         return value<a.value;
    14     }
    15 }e[MAX_N];
    16 void Add_Edge(int u,int v,int w){
    17     e[++tot].from=u;e[tot].to=v;e[tot].value=w;
    18 }
    19 int find(int x){
    20     if(x==fa[x]) return x;
    21     else return fa[x]=find(fa[x]);
    22 }
    23 void Kursual(){
    24     for(int i=1;i<=n;i++)fa[i]=i;
    25     sort(e+1,e+tot+1);
    26     int cur=0;
    27     for(int i=1;i<=tot;i++){
    28         int rx=find(e[i].from),ry=find(e[i].to);
    29         if(rx!=ry){
    30             cur++;fa[rx]=ry;
    31             MST+=e[i].value;
    32         }
    33         if(cur == n-1)break;
    34     }
    35     printf("%lld
    ",MST);
    36 }
    37 int main()
    38 {
    39     while(scanf("%d",&n)){
    40         if(n==0) break;
    41         tot=0;MST=0; 
    42         for(int i=1,u,v,w;i<=n*(n-1)/2;i++){
    43             scanf("%d%d%d",&u,&v,&w);
    44             Add_Edge(u,v,w);Add_Edge(v,u,w);
    45         }
    46         Kursual();
    47     }
    48     return 0;
    49 }

    思路:最小生成树Kursual裸题

  • 相关阅读:
    获取Enum枚举值描述的几法方法
    c# 快速验证代理IP是否有用
    解析JSON对象与字符串之间的相互转换
    MVC4相关Razor语法以及Form表单(转载)
    MVC4数据注解和验证
    C#在Json反序列化中处理键的特殊字符
    EasyUI 兼容 IE6 方法总结
    uploadify在IE6下的问题
    Could not resolve dependencies for project, Failed to read artifact descriptor for
    shell 文本单词计数
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6376702.html
Copyright © 2011-2022 走看看