#include<bits/stdc++.h>
using namespace std; struct node{
int u, v, w, nex;
bool gone; node(){}
node(int a,int b,int c){
u = a;v = b;w = c;gone = false;
}
bool operator <(const node&a)const{
return w < a.w;
}
};
const int maxE = ;
const int maxP = ;
node Gra[maxE];
int dp[maxP], F[maxP];
int T,n,m;
long long ans;
double edgeSum;
vector<pair<int, int> >mp[maxP]; int Find(int x){
if(F[x] == x) return x;
return F[x] = Find(F[x]);
} void init(int nn){
for(int i = ; i <= nn; i ++){
F[i] = i;
mp[i].clear();
}
edgeSum = ;
ans = ;
} int dfs(int rt, int no){
int point = ;
for(int i = mp[rt].size() - ; i >=; i --){
int to = mp[rt][i].first;
int w = mp[rt][i].second;
if(to == no) continue;
point += dfs(to, rt);
double temp = ((double)dp[to] * (double)(n - dp[to]) * w);
edgeSum += temp;
}
dp[rt] = point;
return point;
} int main(){
int w,v,u;scanf("%d",&T);
while(T --){
scanf("%d%d",&n,&m);
init(n);
for(int i = ; i < m; i ++){
scanf("%d%d%d",&Gra[i].u,&Gra[i].v,&Gra[i].w);
}
sort(Gra, Gra + m);
for(int i = ; i < m; i ++){
u = Gra[i].u;v = Gra[i].v;w = Gra[i].w;
int fina = Find(u), finb = Find(v);
if(fina != finb){
F[fina] = finb;
ans = ans + w;
mp[u].push_back(make_pair(v,w));
mp[v].push_back(make_pair(u,w));
}
}
dfs(, );
printf("%lld %.2lf\n",ans,edgeSum*2.0/n/(n - ));
}
return ;
}

Abandoned country(最小生成树+树形DP)的更多相关文章

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

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

  2. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  3. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

  4. Install Air Conditioning HDU - 4756(最小生成树+树形dp)

    Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...

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

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

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

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

  7. 【HDU - 4340】Capturing a country(树形DP)

    BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...

  8. HDU 5723 Abandoned country(最小生成树 + 树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...

  9. hdu4126Genghis Khan the Conqueror (最小生成树+树形dp)

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) Total Submiss ...

随机推荐

  1. 离线应用与客户端存储(cookie storage indexedDB)

    离线检测 HTML5定义一个属性:navigator.onLine的属性.这个属性值为true,表示设备在线,值为false,表示设备离线.为了更好的确定网络是否可用,HTML5还定义了两个事件.这两 ...

  2. HBase系列文章(转)

    HBase概念学习(一)基本架构 HBase概念学习(二)JAVA API操作概览 HBase概念学习(三)Java API之CRUD(增查改删) HBase概念学习(四)Java API之扫描和过滤 ...

  3. linux根文件系统制作,busybox启动流程分析

    分析 busybox-1.1.6 启动流程,并 制作一个小的根文件系统 源码百度云链接:https://pan.baidu.com/s/1tJhwctqj4VB4IpuKCA9m1g 提取码 :l10 ...

  4. 【JMeter】前置处理器

     BeanShell PreProcessor  使用BeanShell在请求进行之前进行操作.语法使用与BeanShell Sampler是一样的.但可使用的内置变量稍有不同    JDBC Pre ...

  5. 接口测试工具-Jmeter使用笔记(三:管理请求服务器信息和Headers参数)

    如果使用Jmeter同时执行多个http请求任务,就需要创建多个HTTP取样器,每一个取样器都来手动填写服务器信息和端口号,会非常消耗时间. 解决方法:Jmeter之HTTP请求默认值 1.添加方式 ...

  6. RN-系列

    8081端口被占用,McAfee Agent关不掉 sudo lsof -n -i4TCP:8081 sudo launchctl list | grep 5693 sudo launchctl re ...

  7. vue-底部导航栏

    <template> <div class="bottom"> <div class="bottom_button iconfont ico ...

  8. numpy的searchsorted细品

      import numpy as np a= np.arange(20) pos_left = a.searchsorted(3)    #也可以写成np.searchsorted(a, 3), 注 ...

  9. Pro*C介绍

    内嵌SQL 概要 Pro*C语法 SQL 预处理指令 语句标号 宿主变量 基础 指针 结构 数组 指示器变量 数据类型同等化 动态SQL 事务 错误处理 SQLCA WHENEVER语句 Demo程序 ...

  10. 产品设计教程:wireframe,prototype,mockup到底有何不同?

    wireframe,prototype,mockup 三者经常被混用,很多人把三者都叫原型,真的是这样吗? 我们来看看三者到底有何不同.先来做一道选择题: 从这张图可以看出,prototype 和其他 ...