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

分析:Kruscal算法求最短路并建树,然后每条边对答案的贡献是分别两个端点及其外部的点的个数

   的乘积乘上边长/总可能情况数,dfs回溯求解点的个数;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e6+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,p[maxn],vis[maxn],cnt;
double ans[2];
vector<pii>edge[maxn];
struct node
{
    int a,b,l;
}q[maxn];
bool cmp(const node&x,const node&y)
{
    return x.l<y.l;
}
int all(int x){return p[x]==x?x:p[x]=all(p[x]);}
int dfs(int now)
{
    int son=1,son_son;
    vis[now]=1;
    for(auto x:edge[now])
    {
        if(!vis[x.fi]){
            son_son=dfs(x.fi);
            son+=son_son;
            ans[1]+=1.0*son_son*(n-son_son)*x.se;
        }
    }
    return son;
}
int main()
{
    int i,j,k,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        rep(i,1,n)p[i]=i,edge[i].clear();
        memset(vis,0,sizeof(vis));
        ans[0]=ans[1]=0;cnt=0;
        rep(i,0,m-1)scanf("%d%d%d",&q[i].a,&q[i].b,&q[i].l);
        sort(q,q+m,cmp);
        rep(i,0,m-1)
        {
            int u=all(q[i].a),v=all(q[i].b);
            if(u!=v)
            {
                p[u]=v;
                ans[0]+=q[i].l;
                edge[q[i].a].pb({q[i].b,q[i].l}),edge[q[i].b].pb({q[i].a,q[i].l});
                cnt++;
                if(cnt==n-1)break;
            }
        }
        dfs(1);
        double y=1.0*(n-1)*n/2;
        printf("%.0f %.2f\n",ans[0],ans[1]/y);
    }
    //system ("pause");
    return 0;
}

n(n≤100000)

时间: 2024-10-12 02:32:59

Abandoned country的相关文章

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

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(最小生成树,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

最小生成树 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<

2016年暑期多校【1】

1.[HDU 5723]Abandoned country(最小生成树+树形dp) 题意:n(n≤100000)个城市,m(m≤1000000)条路,要修建(n-1)条路使这n个城市连通,题目中告诉我们修建每条路的费用,需要求两个问题:1.修建(n-1)条路的最小费用 2.路修建好了之后,任意两点之间的长度的期望 解题思路: 修建(n-1)条路的最小费用==>最小生成树 路修建好了之后,任意两点之间的长度的期望:实际上就是求任意两点之间的平均距离.对于边X,求所有可能的路径经过此边的次数:设这条

HDU_5723_最小生成树+任意两点距离的期望

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

ACM学习之路___HDU 5723(kruskal + dfs)

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