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;
    }
    

      

  • 相关阅读:
    Matlab-9:中心差分方法解常微分算例(SOR完整版)
    Matlab-8:松弛迭代法(SOR)
    Matlab-6:解非线性方程组newton迭代法
    Matlab-5:牛顿迭代法工具箱
    javascript学习笔记--经典继承、组合继承、原型式继承、寄生继承以及寄生组合继承
    爬取微信文章代码
    吴恩达“机器学习”——学习笔记八
    吴恩达“机器学习”——学习笔记七
    吴恩达“机器学习”——学习笔记六
    吴恩达“机器学习”——学习笔记五
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5687291.html
Copyright © 2011-2022 走看看