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 } 
  • 相关阅读:
    labview 中的一些简写全称
    socket
    putty
    在波形图表中显示多条曲线
    简单的通电延时触发电路
    Linux sed 批量替换多个文件中的字符串
    PhpMyAdmin管理,登录多台远程MySQL服务器
    MySQL客户端工具推荐
    Redis的几个认识误区
    Redis 的 5 个常见使用场景
  • 原文地址:https://www.cnblogs.com/yzhhh/p/9954358.html
Copyright © 2011-2022 走看看