zoukankan      html  css  js  c++  java
  • HDU 5176 The Experience of Love

    The Experience of Love

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 145    Accepted Submission(s): 61


    Problem Description
    A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting of N cities and only N - 1  edges (just like a tree), every edge has a value means the distance of two cities. They select two cities to live,Gorwin living in a city and Vivin living in another. First date, Gorwin go to visit Vivin, she would write down the longest edge on this path(maxValue).Second date, Vivin go to Gorwin, he would write down the shortest edge on this path(minValue),then calculate the result of maxValue subtracts minValue as the experience of love, and then reselect two cities to live and calculate new experience of love, repeat again and again.

    Please help them to calculate the sum of all experience of love after they have selected all cases.
     
    Input
    There will be about 5 cases in the input file.
    For each test case the first line is a integer n , Then follows n - 1 lines, each line contains three integers a , b, and c  , indicating there is a edge connects city a and city b with distance c .


    [Technical Specification]
    1<N<=150000,1<=a,b<=n,1<=c<=109
     
    Output
    For each case,the output should occupies exactly one line. The output format is Case #x: answer, here x is the data number, answer is the sum of experience of love.
     
    Sample Input
    3
    1 2 1
    2 3 2
    5
    1 2 2
    2 3 5
    2 4 7
    3 5 4
     
    Sample Output
    Case #1: 1
    Case #2: 17
    Hint
    huge input,fast IO method is recommended. In the first sample: The maxValue is 1 and minValue is 1 when they select city 1 and city 2, the experience of love is 0. The maxValue is 2 and minValue is 2 when they select city 2 and city 3, the experience of love is 0. The maxValue is 2 and minValue is 1 when they select city 1 and city 3, the experience of love is 1. so the sum of all experience is 1;

    求树上任意两点路径上边权的 ( 最大值 - 最小值 ) 之和。

    先排好序 , 用并查集,一条条边加进去 。 然后一条条边减掉 。

    对于并查集 :

    正确简洁的写法 , int find( int k ) { return fa[k] = ( k ==fa[k] ? k : find(fa[k])); }

            少了fa[k]赋值的话会TLE。

    然后这题会爆LL ,

            对于ULL , 可以用 cout , %I64u ,  %llu 输出 。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <vector>
    #include <queue>
    
    using namespace std ;
    typedef long long LL ;
    typedef unsigned long long ULL ;
    typedef pair<int,int> pii;
    #define X first
    #define Y second
    
    const int N = 150010;
    struct node {
        int u , v , w ;
        bool operator < ( const node &a )const {
            return w < a.w ;
        }
    }e[N];
    
    int n , fa[N] ,cnt[N] ;
    void init() { for( int i = 1 ; i <= n ; ++i ) fa[i] = i , cnt[i]= 1 ; }
    int myfind( int k ) { return fa[k] = ( k == fa[k] ? k : myfind(fa[k]) ) ; }
    
    void solve() {
        init();
        sort( e + 1 , e + n );
        ULL sum = 0 , t = 1 ;
        for( int i = 1 ; i < n ; ++i ) {
            int fx = myfind( e[i].u ) , fy = myfind( e[i].v );
            sum += 1ULL * cnt[fx] * cnt[fy] * e[i].w ;
            fa[fx] = fy ; cnt[fy] += cnt[fx] ;
        }
        init();
        for( int i = n - 1 ; i >= 1 ; --i ) {
            int fx = myfind( e[i].u ) , fy = myfind( e[i].v );
            sum -= 1ULL * cnt[fx] * cnt[fy] * e[i].w ;
            fa[fx] = fy ; cnt[fy] += cnt[fx] ;
        }
        printf("%llu
    ",sum);
    }
    int main ()  {
        int _ , cas =1 ;
        while( ~scanf("%d",&n) ) {
            printf("Case #%d: ",cas++);
            for( int i = 1 ; i < n ; ++i ) {
                scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
            }
            solve();
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    R语言nest_join()函数
    R语言行/列合并
    ffmpeg 命令将视频转化为图帧
    Natas Wargame Level20 Writeup(会话状态注入/篡改)
    vim简明教程--半小时从入门到精通
    笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并
    【Ecstore2.0】导出问题解决(未导出或导出文件为0字节)
    Android Handler消息处理顺序分析
    着色器语言之变量类型
    Redis源码分析(十一)--- memtest内存检测
  • 原文地址:https://www.cnblogs.com/hlmark/p/4292868.html
Copyright © 2011-2022 走看看