hdu 5723 Abandoned country(最小生成树,dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723

Abandoned country

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

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

Author

HIT

Source

2016 Multi-University Training Contest 1

题意:

给出n个点和m条边,每条边的权值都不相同,求使得n个点连通而边的权值之和最小,以及随机选2个点作为起点和终点的路径长度的期望

题解:

最小权值直接求最小生成树就好

m条边权值各不相同,所以每个权值对应一个期望,求出最小生成树的时候,期望也一定是最小的

期望是等于每条边的贡献之和除以总路径。

每条边两边的点连通都要经过这条边,所以这条边的贡献就是s(其中一边的点的数)*(n-s)*w(权值);

求每条边的一边的点,可以利用dfs

源代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,m;
struct node
{
    int x,y,w;
    node(int xx,int yy,int ww):x(xx),y(yy),w(ww) {}
};
vector<node> a;
struct Node
{
    int y,w;
    Node(int yy,int ww):y(yy),w(ww) {}
};
vector<Node> b[maxn];
int f[maxn];
bool cmp(const node &i,const node &j)
{
    return i.w<j.w;
}
int finds(int x)
{return f[x] == x?x:f[x] = finds(f[x]);}
long long Kruskal()
{
    long long ans=0;
    for(int i=1; i<=n; i++) f[i]=i;
    sort(a.begin(),a.end(),cmp);
    for(int i=0; i<a.size(); i++)
    {
        int x=finds(a[i].x);
        int y=finds(a[i].y);
        if(x==y)continue;
        f[x]=y;
        ans+=a[i].w;
        b[a[i].x].push_back(Node(a[i].y,a[i].w));
        b[a[i].y].push_back(Node(a[i].x,a[i].w));
    }
    return ans;
}
int s[maxn];
double d[maxn];
void dfs(int root,int father)
{
    s[root]=1;
    for(int i=0; i<b[root].size(); i++)
    {
        int son=b[root][i].y;
        int l=b[root][i].w;
        if(son==father) continue;
        dfs(son,root);
        s[root]+=s[son];
        d[root]+=d[son]+((double)s[son]*(n-s[son]))*l;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        a.clear();
        for(int i=0; i<m; i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            a.push_back(node(x,y,w));
        }
        memset(s,0,sizeof(s));
        memset(d,0,sizeof(d));
        for(int i=1; i<=n; i++)
            b[i].clear();
        long long ans=Kruskal();
        dfs(1,0);
        long long g=(long long)n*(n-1)/2;
        printf("%I64d %.2lf\n",ans,d[1]/(double)g);
    }
    return 0;
}
时间: 2024-11-07 21:43:25

hdu 5723 Abandoned country(最小生成树,dfs)的相关文章

HDU 5723 Abandoned country 最小生成树+搜索

Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4477    Accepted Submission(s): 1124 Problem Description An abandoned country has n(n≤100000) villages which are numbered from 1

hdu 5723 Abandoned country 最小生成树+子节点统计

Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3006    Accepted Submission(s): 346 Problem Description An abandoned country has n(n≤100000) villages which are numbered from 1

最小生成树 kruskal hdu 5723 Abandoned country

题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************************************************************** Problem:hdu 5723 User: youmi Language: C++ Result: Accepted Time:2932MS Memory:22396K solution:首先注意到任

HDU 5723 Abandoned country

题目说每条边权值都不一样,说明最小生成树是唯一的,不存在最小期望这一说. 然后就是先求出最小生成树,随便确定一个根节点,计算出每个点的子树有多少节点,记为c[x]. 指向x的这条边被统计的次数为c[x]*(n-c[x]).然后基本就可以算出答案了. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<

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

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). Guarante

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

Abandoned country

Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 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

HDU 哈密顿绕行世界问题 (dfs)

Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出. Output 输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output

HDU 4786 Fibonacci Tree 最小生成树变形

思路: 这题比赛的时候想了好久,最后队友机智的想到了. 不过那时不是我敲的,现在敲的1A. 想好就容易了. 直接把1或者0当做边的权值,然后按边从小到大排序,然后算最小生成用到了几条白边,然后再按边从大到小排序,然后再算白边用了几条.然后最小和最大需要用到的白边都算出来了.如果在这最小最大区间中存在那个啥数列的话就是Yes,否则就是No. 为什么在这区间里面就是对的呢?刚开始我也想了好久,然后发现,因为白边权值是1,然后黑边是0,然后假设用到白边最小的是6,最大的是10,那么,我们可以用黑边去替