首页 > 代码库 > HDU5723 Abandoned country 最小生成树+深搜回溯法

HDU5723 Abandoned country 最小生成树+深搜回溯法

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

 

题意:
  给定n个定点,m条边,求最小生成树以及任意两点之间距离的期望。
思路:
  任意两点的期望是(权值)*(这条路被走过的次数)的总和   除以  总共的路径数。
  这里不能求最短路,应该用深搜回溯,找出这条路被走过过少次。这样就可以求出期望了。
  这里由于数据量比较大,不能用prim数组求最小生成树,可以用Kruskal求最小生成树。
  坑点:注意最后求期望的时候总共的路径数量n*(n-1)/2数据量比较大,应该用long long和double存储。
/*
3
0 0
3 3
1 2 1
2 3 2
1 3 5
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
*/
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<string.h>
using namespace std;
vector<pair<int,int> >  v[100010];

struct Edge
{
    int f,t,q;
};

int m,n;//n为村庄数,m为街道数
Edge s[1000010];//存储图
long long  ans;//存最后的每条路的总和
int pre[100010];//并查集的祖先数组
int vis[100010];//标记数组

bool cmp(Edge a,Edge b )//排序函数
{
    return a.q<b.q;
}

int Find(int x)//找祖先
{
    if(x!=pre[x])
    {
        pre[x]=Find(pre[x]);
    }
    return pre[x];
}

void Merge(int x,int y)//查是否相等
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
        pre[fx]=fy;
}

long long dfs(int x) //dfs递归搜索
{
    vis[x]=1;
    long long now=0,all=1;//now记录当前节点直接连接的节点数量  all记录此节点经过搜索后所有的与此节点连接的节点数
    int h=v[x].size();
    for(int i=0; i<h; i++)
    {
        int b=v[x][i].first;
        if(!vis[b])
        {
            now=dfs(b);
            all+=now;
            ans+=now*(n-now)*v[x][i].second;//ans记录的是权值*经过的次数
        }
    }
    return all;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        if(m==0||n==0)
            {printf("0 0.00");continue;}

        for(int i=0;i<=n;i++)
            v[i].clear();
        for(int i=0; i<=n; i++)//并查集的祖先节点的初始化
        {
            pre[i]=i;
        }
        for(int j=0; j<m; j++)//输入
        {
            scanf("%d%d%d",&s[j].f,&s[j].t,&s[j].q);
        }
        sort(s,s+m,cmp);//排序
        long long sum=0;//sum用来记录最小生成树的长度
        for(int j=0; j<m; j++)
        {
            int fx=Find(s[j].f);
            int fy=Find(s[j].t);
            if(fx!=fy)  //如果祖先不相等,那么加入到最小生成树中
            {
                sum=sum+s[j].q;
                Merge(fx,fy);
                //加入到动态数组中准备做期望
                v[s[j].f].push_back(make_pair(s[j].t,s[j].q));
                v[s[j].t].push_back(make_pair(s[j].f,s[j]. q));
            }
        }
        dfs(1);//深搜回溯计算ans的值
        double y=1.0*n*(n-1)/2;
        printf("%lld %.2lf\n",sum,(double)ans/y);
    }
    return 0;
}

 

 

 

HDU5723 Abandoned country 最小生成树+深搜回溯法