hdu5723 最小生成树 树形dp

来源:互联网 发布:创意马克杯 知乎 编辑:程序博客网 时间:2024/04/30 22:45

Abandoned country

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


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
14 61 2 12 3 23 4 34 1 41 3 52 4 6
 

Sample Output
6 3.33
 

Author
HIT

题意:国王有n个城市,计划修建一些路要把所有的城市连接起来,他有一些方案,让我们计算最小花费的方案
,而后,在最小花费的方案上计算一个期望,啥子期望腻,就是国王要随机选择两个城市,问这两个城市之间距离的期望,既任意两个城市都有可能被选择到,那么期望等于所有路程和/路径个数,最小花费方案不用说肯定是最小生成树了,既然是最小生成树,那么路径个数= n*(n-1)/2,路程长度咋求啊,不能把所有路径都求出来加和啊,那么我们想到,可以算每条路用的次数,我们发现,一条路用的次数等于这条路两侧城市个数之积;
那么,我们就可以用一遍深搜,求出每个节点的孩子个数,那么一条边 u-->v    u,v一个是孩子,一个是父亲
父亲的孩子比孩子的孩子多,所以用孩子的孩子个数计算,那么这条边的利用次数:  tp=min(child[u],child[v])
次数 = (n-tp)*tp 


#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <stdlib.h>#include <vector>#include <queue>const int maxm=1000000+1000,maxn=100000+100;using namespace std;struct node{    int x,y;    long long num;}d[maxm];bool cmp(node a,node b){    return a.num<b.num;}int par[maxn];int rank2[maxn];void init1(int n){    for(int i=0;i<=n;i++)    {        par[i]=i;        rank2[i]=0;    }}int find_(int x){    if(par[x]==x)        return x;    else    {      return par[x]=find_(par[x]);    }}void add(int x,int y){    x=find_(x);    y=find_(y);    if(x==y)    {        return;    }    if(rank2[x]<rank2[y])    {        par[x]=y;    }    else{        par[y]=x;        if(rank2[x]==rank2[y])            rank2[x]++;    }}struct Edge{    int v;    long long w;    Edge(){;}    Edge(int vv,long long ww)    {      v=vv;      w=ww;    }};int leaf[maxn];vector<Edge>G[maxm];void init2(int n){   for(int i=0;i<=n;i++)   {        G[i].clear();        leaf[i]=1;   }}void add_edge(int u,int v,long long w){    G[u].push_back(Edge(v,w));    G[v].push_back(Edge(u,w));}void dfs(int u,int pre){    if(G[u].size()==1)        leaf[u]=1;    int num=G[u].size();    for(int i=0;i<num;i++)    {        int v=G[u][i].v;        if(v==pre)            continue;        dfs(v,u);        leaf[u]+=leaf[v];    }}int vis[maxn];int N;double bfs(int s){    memset(vis,0,sizeof(vis));    queue<int>que;    que.push(s);    double  ans=0.0,fm,temp;    fm=1.0*N*(N-1)/2.0;    //cout<<"fm"<<fm<<endl;    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=1;        for(int i=0;i<G[u].size();i++)        {            int v=G[u][i].v;            if(!vis[v])            {                que.push(v);                 temp=1.0*min(leaf[u],leaf[v]);                ans=ans+(((1.0*N-temp)*temp)*(1.0*G[u][i].w))/fm;                //cout<<ans<<endl;            }        }    }    return ans;}int main(){    int t;    int n,m;    long long mintree;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        init2(n);        init1(n);        N=n;        mintree=0;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&d[i].x,&d[i].y,&d[i].num);        }        sort(d,d+m,cmp);        for(int i=0;i<m;i++)        {            if(find_(d[i].x)!=find_(d[i].y))            {                add(d[i].x,d[i].y);                mintree+=d[i].num;                add_edge(d[i].x,d[i].y,d[i].num);            }        }        dfs(1,1);        double ans2=bfs(1);        printf("%lld %.2lf\n",mintree,ans2);    }    return 0;}



 

0 0