【HDU】5723

来源:互联网 发布:西北工业大学矩阵论 编辑:程序博客网 时间:2024/06/03 14:35

题目链接:点击打开题目


Abandoned country

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

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


题意:给出一个地图,用最短的花费把顶点连起来(最小生成树),然后求这里面任意两点最短路的期望。


题解:最小生成树的地方没什么好说的,把图记录下来就好。求期望的时候要遍历树,数据结构我的树学的还不错,求出最短路的总和除以总数就得到了结果了。


代码如下:

#include<map>#include<queue>#include<cmath>#include<stack>#include<cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))#define PI acos(-1.0)struct Edge{    int x,y,length;    bool friend operator < (Edge a,Edge b)    {        return a.length < b.length;    }}edge[1000000+5];LL sum = 0;     //总时间 int n,m;        //顶点数,边数 int f[1000000+5];vector<pair<int,int> > d[1000000+5];int find(int x){    if (x != f[x])        f[x] = find(f[x]);    return f[x];}bool join(int x,int y){    int fx = find(x);    int fy = find(y);    if (fx != fy)    {        f[fx] = fy;        return true;    }    return false;}LL dfs(int father,int pos){    LL ant = 0;    for (int i = 0 ; i < d[pos].size() ; i++)    {        if (d[pos][i].first != father)        {            LL cnt = dfs(pos,d[pos][i].first);            sum += cnt * ((LL)n-cnt) * d[pos][i].second;            ant += cnt;        }    }    return ant+1;       //加上自己 }int main(){    int T;    LL MinLength;    double MinExpectation;    scanf ("%d",&T);    while (T--)    {        scanf ("%d%d",&n,&m);        for (int i = 1 ; i <= n ; i++)        {            f[i] = i;            d[i].clear();        }        MinLength = 0;        for (int i = 0 ; i < m ; i++)            scanf ("%d %d %d",&edge[i].x,&edge[i].y,&edge[i].length);        sort(edge,edge+m);        for (int i = 0 , ant = 0 ; i < m ; i++)        {            if (join(edge[i].x,edge[i].y))            {                MinLength += edge[i].length;                d[edge[i].x].push_back(make_pair(edge[i].y,edge[i].length));                d[edge[i].y].push_back(make_pair(edge[i].x,edge[i].length));                ant++;            }            if (ant == n-1)     //优化一下                 break;        }        cout << MinLength << ' ';        //对树进行搜索        sum = 0;        for (int i = 0 ; i < d[1].size() ; i++)        {            LL cnt = dfs(1,d[1][i].first);      //下面的节点数             sum += cnt * ((LL)n-cnt) * d[1][i].second;        }        printf ("%.2lf\n",(double)sum*2.0/((double)n*(n-1)));    }    return 0;}
原创粉丝点击