zoukankan      html  css  js  c++  java
  • HDU-2988-Dark roads(最小生成树)

    Problem Description
    Economic times these days are tough, even in Byteland. To reduce the operating costs, the government of Byteland has decided to optimize the road lighting. Till now every road was illuminated all night long, which costs 1 Bytelandian Dollar per meter and day. To save money, they decided to no longer illuminate every road, but to switch off the road lighting of some streets. To make sure that the inhabitants of Byteland still feel safe, they want to optimize the lighting in such a way, that after darkening some streets at night, there will still be at least one illuminated path from every junction in Byteland to every other junction.

    What is the maximum daily amount of money the government of Byteland can save, without making their inhabitants feel unsafe?

    Input
    The input file contains several test cases. Each test case starts with two numbers m and n, the number of junctions in Byteland and the number of roads in Byteland, respectively. Input is terminated by m=n=0. Otherwise, 1 ≤ m ≤ 200000 and m-1 ≤ n ≤ 200000. Then follow n integer triples x, y, z specifying that there will be a bidirectional road between x and y with length z meters (0 ≤ x, y < m and x ≠ y). The graph specified by each test case is connected. The total length of all roads in each test case is less than 231.
     

    Output
    For each test case print one line containing the maximum daily amount the government can save.
     

    Sample Input
    7 11 0 1 7 0 3 5 1 2 8 1 3 9 1 4 7 2 4 5 3 4 15 3 5 6 4 5 8 4 6 9 5 6 11 0 0
     

    Sample Output
    51


    思路:克鲁斯卡尔算法。这个题比较水。


     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int m,n;
     5 int pre[200005];
     6 struct node{
     7     int a,b,len;
     8 }l[200005];
     9 
    10 int cmp(node x,node y){
    11     return x.len<y.len;
    12 }
    13 int find(int x){
    14     while(pre[x]!=x){
    15         int r=pre[x];
    16         pre[x]=pre[r];
    17         x=r;
    18     }
    19     return x;
    20 }
    21 
    22 void merge(int x,int y){
    23     int fx=find(x);
    24     int fy=find(y);
    25     if(fx!=fy) pre[fx]=fy;
    26 }
    27 int main(){
    28     while(scanf("%d%d",&m,&n)){
    29         if(m==0&&n==0) break;
    30         
    31         for(int i=0;i<m;i++)
    32             pre[i]=i;
    33         int ysum=0;    
    34         for(int i=0;i<n;i++){
    35             scanf("%d%d%d",&l[i].a,&l[i].b,&l[i].len);
    36             ysum+=l[i].len;
    37         }
    38         sort(l,l+n,cmp);
    39         int sum=0;
    40         for(int i=0;i<n;i++){
    41             if(find(l[i].a)==find(l[i].b)) continue;
    42             sum+=l[i].len;
    43             merge(l[i].a,l[i].b);            
    44         }
    45         
    46         printf("%d
    ",ysum-sum);    
    47         
    48     }
    49     return 0;
    50 } 
  • 相关阅读:
    使用 Prism.js 实现代码高亮
    Win10系统如何删除网络及修改网络名称
    CRT和PEM格式证书转换
    CentOS如何修改主机名
    crt格式证书转换为pem格式
    CentOS安装wget命令
    自适应(响应式)网页中的几个关键分辨率
    java中的I/O流学习(1)
    有种心态,我不知该如何表达
    java学习笔记—Scanner
  • 原文地址:https://www.cnblogs.com/yzhhh/p/9954358.html
Copyright © 2011-2022 走看看