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+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,p[maxn],vis[maxn],cnt;
double ans[];
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=,son_son;
vis[now]=;
for(auto x:edge[now])
{
if(!vis[x.fi]){
son_son=dfs(x.fi);
son+=son_son;
ans[]+=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,,n)p[i]=i,edge[i].clear();
memset(vis,,sizeof(vis));
ans[]=ans[]=;cnt=;
rep(i,,m-)scanf("%d%d%d",&q[i].a,&q[i].b,&q[i].l);
sort(q,q+m,cmp);
rep(i,,m-)
{
int u=all(q[i].a),v=all(q[i].b);
if(u!=v)
{
p[u]=v;
ans[]+=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-)break;
}
}
dfs();
double y=1.0*(n-)*n/;
printf("%.0f %.2f\n",ans[],ans[]/y);
}
//system ("pause");
return ;
}
n(n≤100000)

Abandoned country的更多相关文章

  1. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

  2. HDU 5723 Abandoned country(落后渣国)

    HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  3. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  4. HDU5723 Abandoned country (最小生成树+深搜回溯法)

    Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...

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

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

  7. hdu 5723 Abandoned country 最小生成树 期望

    Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  8. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...

  9. HDU 5723:Abandoned country(最小生成树+算期望)

    http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Problem Description   An abandoned ...

随机推荐

  1. sd卡文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  2. C相关的图书(链接不可用)

    Visual C++视频技术方案宝典.pdf: http://www.t00y.com/file/17628500 Windows 图形编程.pdf: http://www.t00y.com/file ...

  3. MFC中为菜单或按钮添加快捷键功能

    1.新建一快捷键资源,ACCELERATOR,关联相应的ID号,下图所示中,其中,第一个ID为自定义快捷键ID,按CTRL+R,此时响应该ID以应的消息响应函数, 第二个ID为菜单ID,此时按CTRL ...

  4. python 常用

    1. dir()             不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__(),该方法将被调用.如果参数不包含 ...

  5. 循环语句 ,for语句

    for语句主要用来反复执行某段代码: for(初始条件:循环条件:状态改变) { 循环体 } 问题类型: 1.穷举(例:0-100以内与7有关的数) <body><input typ ...

  6. Python 学习笔记2

    今天继续安装配置python. Fear can hold you prisoner. Hope can set you free.

  7. 经常出现null错误之tostring

    如果需要转换的类型可能为null,如果使用tostring就可能引发错误,这时候可以使用convert.tostring方法.

  8. jersey inject

    http://stackoverflow.com/questions/27665744/how-to-inject-an-object-into-jersey-request-context

  9. 笨方法学python--第一个程序

    该章主要知识点有: 1 print 打印,有双引号,单引号 2 分析报错信息,积累经验 3 # -*- coding:utf-8 -*-,可以输出汉字 4 井号,# ,注释, 英文名 octothor ...

  10. UITextfield的一些属性

    //设置左视图 不用设置frame UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&quo ...