zoukankan      html  css  js  c++  java
  • hdu-5723 Abandoned country(最小生成树+期望)

    题目链接:

    Abandoned country

    Time Limit: 8000/4000 MS (Java/Others)   

     Memory Limit: 65536/65536 K (Java/Others)


    Problem Description
     
    An 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 wi 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.
     
    Input
     
    The 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,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
     
    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
     
    Sample Output
     
    6 3.33
     
    题意:
     
    在一个无向图上找一棵最小生成树,再求一下任意两点距离的期望;
     
    思路:
     
    找最小生成树,找期望的时候看每条边对答案的贡献;
     
    AC代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <bits/stdc++.h>
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e6+10;
    const int maxn=1e5+10;
    const double eps=1e-5;
    
    
     int n,m,head[maxn],p[maxn],num[maxn],cnt;
     int findset(int x)
     {
     	if(x==p[x])return x;
     	return p[x]=findset(p[x]);
     }
    struct Edge
    {
    	int from,to,next,val;
    }edge[N];
    
    int cmp(Edge a,Edge b)
    {
    	return a.val<b.val;
    }
    struct node
    {
    	int to,val;
    };
    vector<pair<int,int> >ve[maxn];
    void add_edge(int s,int e,int va)
    {
    	edge[cnt].from=s;
    	edge[cnt].to=e;
    	edge[cnt].next=head[s];
    	edge[cnt].val=va;
    	head[s]=cnt++;
    }
     LL dis=0;
     int dfs(int cur,int fa,int val)
     {
     	int len=ve[cur].size();
     	num[cur]=1;
     	For(i,0,len-1)
     	{
     		int x=ve[cur][i].first,va=ve[cur][i].second;
     		if(x==fa)continue;
     		num[cur]+=dfs(x,cur,va);
     	}
     	dis=dis+(LL)num[cur]*(n-num[cur])*val;
     	return num[cur];
     }
    int main()
    {
    
    		int t;
    		read(t);
    		while(t--)
    		{
    			cnt=0;
    			dis=0;
    			read(n);read(m);
    			For(i,0,n)
    			{
    				head[i]=-1;
    				p[i]=i;
    				ve[i].clear();
    			}
    			For(i,1,m)
    			{
    				int u,v,w;
    				read(u);read(v);read(w);
    				add_edge(u,v,w);
    			}
    			sort(edge,edge+m,cmp);
    			LL sum=0;
    			For(i,0,m-1)
    			{
    				int fa=findset(edge[i].from),fb=findset(edge[i].to);
    				if(fa!=fb)
    				{
    					p[fa]=fb;
    					sum=sum+edge[i].val;
    					ve[edge[i].from].push_back({edge[i].to,edge[i].val});
    					ve[edge[i].to].push_back({edge[i].from,edge[i].val});
    				}
    			}
    			dfs(1,0,0);
    			printf("%lld %.2lf
    ",sum,dis*1.0/(0.5*(n-1)*n));
    		}
    
            return 0;
    }
    

      

  • 相关阅读:
    C++Primer第5版学习笔记(三)
    C++Primer第5版学习笔记(二)
    C++Primer第5版学习笔记(一)
    A*寻路算法的探寻与改良(三)
    A*寻路算法的探寻与改良(二)
    A*寻路算法的探寻与改良(一)
    html5页面js判断是否安装app,以及判断是否在app内部打开html5页面
    结合prototype和xmlhttprequest封装ajax请求
    前端常见的性能优化和浏览器兼容性问题
    常见的HTTP状态码
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5687291.html
Copyright © 2011-2022 走看看