Abandoned country HDU - 5723 —— 最小生成树+排列路径和

An abandoned country has n(n≤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≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). 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(T≤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,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
前面一个答案就是最小生成树,由于他的每条边长度是不同的,所以最小生成树就是最短排列路径,(刚开始看错题了,一直卡住),那么我们只要算每一条边用了几次,那每天一条边用的次数是,它的左边a个数中选一个数,右边b个数中选一个数,可能性就是a*b,然后所有可能性就是一个累加,除掉就好了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+5;
int fa[maxn],num[maxn],son[maxn];
int finds(int x)
{
    return x==fa[x]?fa[x]:fa[x]=finds(fa[x]);
}
struct edge
{
    int x,y;
    ll val;
    bool operator< (const edge& x)const
    {
        return val<x.val;
    }
}e[maxn*2];
struct node
{
    int to,next;
    ll val;
}nod[maxn*2];
int cnt,head[maxn];
void add(int x,int y,ll val)
{
    nod[cnt].to=y;
    nod[cnt].next=head[x];
    nod[cnt].val=val;
    head[x]=cnt++;
}
double ans,mul;
int n,m;
void dfs1(int f,int ff)
{
    for(int i=head[f];~i;i=nod[i].next)
    {
        int son=nod[i].to;
        if(son!=ff)
        {
            dfs1(son,f);
            num[f]+=num[son]+1;
            int rest=n-num[son]-1;
            ans+=(double)rest*(num[son]+1)*nod[i].val/mul;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        mul=(double)(n-1)*n/2;
        for(int i=1;i<=n;i++)
            fa[i]=i,num[i]=0;
        for(int i=1;i<=m;i++)
            scanf("%d%d%lld",&e[i].x,&e[i].y,&e[i].val);
        sort(e+1,e+1+m);
        memset(head,-1,sizeof(head));
        cnt=0;
        ll val=0;
        for(int i=1;i<=m;i++)
        {
            int fax=finds(e[i].x);
            int fay=finds(e[i].y);
            if(fax!=fay)
                fa[fay]=fax,add(e[i].x,e[i].y,e[i].val),add(e[i].y,e[i].x,e[i].val),val+=e[i].val;
        }
        int sta;
        for(int i=1;i<=n;i++)
            if(fa[i]==i)
            {
                sta=i;
                break;
            }
        ans=0;
        dfs1(sta,0);
        printf("%lld %.2f\n",val,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82377370
今日推荐