2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题

Abandoned country

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

Problem Description

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

Source

2016 Multi-University Training Contest 1

 

 题目大意:

 ①有n个点,m条边,需要建立一个图,使得花费最小并且每个点之间保证有路通达而且没有环,求最小花费。

 ②建完路之后,随机选出两个点,求其最小期望值。


分析样例:

1、Kruskal算法求最小生成树,然后记录下贪心入的边:

1-2 1 

2-3 2 

3-4 3

其值为1+2+3=6;


2、对应任意选两点之间的最短距离:

1-2 1

1-3 3

1-4 6

2-3 2

2-4 5

3-4 3

那么其分布为:

12356
1/61/61/31/61/6

那么其期望:1*1/6+2*2/6+3*1/3+5*1/6+6*1/6=3.33


分母是Cn取2.

思路:


1、Kruskal算法求出最小生成树的值,这里我觉得不能用Prim直接搞,N^2确实感觉有点吃不消。然后对于贪心入树的边记录下来到图中,然后用这个图进行下一步操作。


2、对应形成的图一定是一颗树(废话ing~~~~~~),那么对应每一条边,其对应选择通过的次数为:Tmp*(N-Tmp),Tmp表示当前弧(u,v)的顶点v其子树(包括顶点v)的节点个数。


e.g:


对应边u,v会选取过:3*3=9次

1-v;1-3;1-4;2-v;2-3;2-4;u-3;u-4;u-v;

那么对应这条边会增加期望:9*W(u,v)==(Tmp*(N-Tmp))*W(u,v);


3、然后根据这个特性,进行一次深搜,将每一种情况都加进去即可。


坑点:


1、记住,千万要加完之后再除Cn*2.......................................否则会出现精度损失的情况,毕竟输出两位有效数字。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long int
struct edge
{
    int from,to,w,next;
}e[1000050];
struct node
{
    int x,y,w,flag;
}a[1000050];
ll pre[100050];
int head[100050];
int vis[100050];
int f[100050];
ll n;
int m,cont;
ll cn2;
double ans;
bool cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
ll Dfs(int u)
{
    vis[u]=1;
    ll cont=1;
    ll tmp;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(vis[v]==0)
        {
            tmp=Dfs(v);
            ans+=tmp*(n-tmp)*1.0*w;
            cont+=tmp;
        }
    }
    return cont;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(vis,0,sizeof(vis));
        memset(pre,0,sizeof(pre));
        memset(head,-1,sizeof(head));
        scanf("%I64d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
        }
        sort(a,a+m,cmp);
        ll output=0;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)
        {
            if(find(a[i].x)!=find(a[i].y))
            {
                merge(a[i].x,a[i].y);
                add(a[i].x,a[i].y,a[i].w);
                add(a[i].y,a[i].x,a[i].w);
                output+=a[i].w;
            }
        }
        ans=0;
        cn2=n*(n-1)/2;
        printf("%I64d ",output);
        Dfs(1);
        printf("%.2f\n",ans*1.0/cn2*1.0);
    }
}
/*
5 4
1 3 3
3 2 2
3 4 1
4 5 4
*/





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值