hdu 5723 最小生成树+期望

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liyunlong41/article/details/52123658

Abandoned country

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


Problem Description
An abandoned country has $n (n \leq 100000)$ villages which are numbered from 1 to $n$. Since abandoned for a long time, the roads need to be re-built. There are $m (m \leq 1000000)$ roads to be re-built, the length of each road is $w_{i} (w_{i} \leq 1000000)$. Guaranteed that any two $w_{i}$ 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 (T \leq 10)$ 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, w_{i}$, the length of a road connecting the village $i$ and the village $j$ is $w_{i}$.
 

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
 

Author
HIT
 

Source
 

题意:求最小生成树,并且在最小生成树的基础上求图中任意选两个点的期望距离。
分析:做最小生成树的过程中直接建图,然后来一遍dfs,统计每个节点的子节点的个数为c,然后答案贡献为:c*这条边的权值*(n-1)*n;

#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=100010,MOD=1e9+7;
double ans;
int pre[N];
int find(int x)
{
    while(pre[x]!=x) x = pre[x] = pre[pre[x]];
    return x;
}

struct st
{
    int l,r;
    ll cost;
    bool operator < (const st & a) const
    {
        return cost < a.cost;
    }
}a[N*10];   //边的数量要多!
vector<pll> G[N];
int n;
void init()
{
    ans = 0;
    for(int i=1;i<=n;i++){
        pre[i]=i;
        G[i].clear();
    }
}

int dfs(int x,int fa)
{
    int son = 0;
    for(int i=0;i<G[x].size();i++){
        if(G[x][i].F == fa) continue;
        pll t = G[x][i];
        int tmp = dfs(t.F,x);
        son += tmp;
        ans += 1.0*tmp*(n-tmp)*t.S; ///////////tmp
    }
    return son+1;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int m;
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++){
            scanf("%d%d%I64d",&a[i].l,&a[i].r,&a[i].cost);
        }
        sort(a,a+m);
        ll minCost = 0;
        for(int i=0;i<m;i++)
        {
            int x = find(a[i].l);
            int y = find(a[i].r);
            if(x == y) continue;
            pre[x]=y;
            G[a[i].l].push_back(pll(a[i].r,a[i].cost));
            G[a[i].r].push_back(pll(a[i].l,a[i].cost));
            minCost += a[i].cost;
        }
        printf("%I64d ",minCost);
        dfs(1,-1);
        ans /= (1.0*n*(n-1)/2.0);
        printf("%.2lf\n",ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/liyunlong41/article/details/52123658