• HDU_5723_最小生成树+任意两点距离的期望


    Abandoned country

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 3449    Accepted Submission(s): 846


    Problem DescriptionAn abandoned country has n(n100000)villages which are numbered from 1 to n . Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000) . Guaranteed that any two w are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.
     
    InputThe first line contains an integer T(T10) which indicates the number of test cases. 

    For each test case, the first line contains two integers n,indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,w , the length of a road connecting the village i and the village j is w .
     
    Output
    output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
     
    Sample Input
    1
    4 6
    1 2 1
    2 3 2
    3 4 3
    4 1 4
    1 3 5
    2 4 6
     

    题意:求最小生成树,再求任意两点距离的期望。

    最小生成树用kruscal。期望一开始不会求,看题解发现使用dfs:分别求每条边的贡献,一条边的贡献等于这条边的|左子树|*|右子树|*value。

    代码中使用pair和vector

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    struct Path
    {
        int x,y;
        double value;
    } path[1000005];
    
    typedef pair<int ,int> P;
    vector <P> edge[1000005];
    
    bool cmp(Path a,Path b)
    {
        return a.value<b.value;
    }
    int father[100005],n,m;;
    
    int find(int x)
    {
        if(x!=father[x])
            father[x]=find(father[x]);
        return father[x];
    }
    
    void merge(int a,int b)
    {
        int x=find(a);
        int y=find(b);
        if(x!=y)
            father[x]=y;
    }
    
    long long sumv=0,sumd=0;
    void kruscal()
    {
        for(int i=1; i<=n; i++)
            father[i]=i;
        for(int i=0; i<=n; i++)
            edge[i].clear();
        sort(path,path+m,cmp);
        for(int i=0; i<m; i++)
        {
            int x=path[i].x,y=path[i].y,value=path[i].value;
            if(find(path[i].x)!=find(path[i].y))
            {
                sumv+=path[i].value;
                merge(path[i].x,path[i].y);
                edge[x].push_back(P(y,value));
                edge[y].push_back(P(x,value));
            }
        }
    }
    
    int dfs(int x,int last)
    {
        int cnt=0;
        for(int i=0;i<edge[x].size();i++)
        {
            int y=edge[x][i].first,value=edge[x][i].second;
            if(last!=y)
            {
                int now=dfs(y,x);
                cnt+=now;
                sumd+=1.0*now*(n-now)*value;
            }
        }
        return cnt+1;
    }
    int main() { int t; scanf("%d",&t); while(t--) { sumv=0; sumd=0; scanf("%d%d",&n,&m); for(int i=0; i<m; i++) { scanf("%d%d%lf",&path[i].x,&path[i].y,&path[i].value); } kruscal(); dfs(1,-1); printf("%I64d %.2lf ",sumv,sumd*2.0/(1.0*n)/(n-1.0)); } return 0; }
    Sample Output
    6 3.33
     
    Author
    HIT
     
    Source
  • 相关阅读:
    动态修改 dom 元素的伪类样式
    visual studio 2013 触发挂起事件
    windows phone 切换多语言时,商店标题显示错误的问题
    01、Windows Store APP 设置页面横竖屏的方法
    HTTPS和HTTP的概念和区别
    前端要知道的浏览器原理
    HTTP协议详解
    angular4路由设置笔记
    js正则及常用方法函数总结
    史上最全常用正则表达式大全
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5702215.html
Copyright © 2020-2023  润新知