• hdu 2988(最小生成树 kruskal算法)


    Dark roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1031    Accepted Submission(s): 450

    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
    Source
    所以边的权值-最小生成树的权值
    裸题
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<cstdlib>
    11 #include<string>
    12 #define eps 0.000000001
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int N=200000+100;
    17 int parent[N];
    18 int ans;
    19 int m,n;
    20 struct node{
    21     int u,v,w;
    22 }a[N];
    23 bool cmp(node x,node y){
    24     return x.w<y.w;
    25 }
    26 void init(){
    27     for(int i=0;i<=N;i++)parent[i]=i;
    28 }
    29 int find(int x){
    30     int r=x;
    31     while(parent[r]!=r)r=parent[r];
    32     int i=x;
    33     int j;
    34     while(i!=r){
    35         j=parent[i];
    36         parent[i]=r;
    37         i=j;
    38     }
    39     return r;
    40 }
    41 void kruskal(){
    42     for(int i=0;i<m;i++){
    43         int x=find(a[i].u);
    44         int y=find(a[i].v);
    45         if(x!=y){
    46             parent[x]=y;
    47             ans=ans+a[i].w;
    48         }
    49     }
    50 }
    51 int main(){
    52     while(scanf("%d%d",&n,&m)!=EOF){
    53         init();
    54         if(m==0&&n==0)break;
    55         int sum=0;
    56         ans=0;
    57         for(int i=0;i<m;i++){
    58             scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
    59             sum=sum+a[i].w;
    60         }
    61         //cout<<sum<<endl;
    62         sort(a,a+m,cmp);
    63         kruskal();
    64 
    65         //cout<<ans<<endl;
    66         printf("%d
    ",sum-ans);
    67     }
    68 }
  • 相关阅读:
    动画效果打开层 关闭层
    一个正在加载网页的进度条,加载完后,自动消失?>
    使用ASP.NET AJAX必要的配置
    css静态滤镜 + A:Hover 的效果
    可擦写的涂改文字
    各种遮罩层(lightbox)实现
    oracle 11g 安装
    SMTP协议
    javaoracle驱动包
    批量处理JDBC语句提高处理速度
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6444074.html
Copyright © 2020-2023  润新知