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

    L - 还是畅通工程
    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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
     
    模板题
     
     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <string.h>
     4 #include <algorithm>
     5 using namespace std;
     6 const int N=11115;
     7 int pa[N],n;
     8 struct node
     9 {
    10     int u,v,w;
    11     bool operator<(const node &C)const
    12     {
    13         return w<C.w;
    14     }
    15 } t[N];
    16 int cha(int x)
    17 {
    18     if(x!=pa[x])
    19     {
    20         pa[x]=cha(pa[x]);
    21     }
    22     return pa[x];
    23 }
    24 int cl()
    25 {
    26     int sum=0,top=0;
    27     for(int i=1; i<=(n*(n-1))/2; i++)
    28     {
    29         int x=t[i].u;
    30         int y=t[i].v;
    31         x=cha(x);
    32         y=cha(y);
    33         if(x!=y)
    34         {
    35             pa[x]=y;
    36             sum+=t[i].w;
    37             top++;
    38         }
    39     }
    40         return sum;
    41 }
    42 int main()
    43 {
    44     while(scanf("%d",&n)!=EOF&&n)
    45     {
    46         for(int j=1; j<=n; j++)
    47         {
    48             pa[j]=j;
    49         }
    50         for(int i=1; i<=(n*(n-1))/2; i++)
    51         {
    52             scanf("%d%d%d",&t[i].u,&t[i].v,&t[i].w);
    53         }
    54         sort(t+1,t+((n*(n-1))/2)+1);
    55         printf("%d
    ",cl());
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    电子书、API、帮助文档 链接收藏
    .net上传图片我自己的可下载资源
    VS2012+EF5学习MVC4(二)
    VS2012+EF5学习MVC4(三)
    MVC 3.0 的新特性
    最受欢迎的ASP.NET的CMS下载
    简易甘特图 .net javascript
    VS2010+EF Code First 4.1学习MVC3(三)
    winform WebBrowser 文件下载拦截 获取文件本地保存路径
    一次偶遇 IOException
  • 原文地址:https://www.cnblogs.com/tianmin123/p/4767872.html
Copyright © 2011-2022 走看看