Abandoned country(最小生成树+树形DP)
- #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)的更多相关文章
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...
- hdu 4081 最小生成树+树形dp
思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...
- Install Air Conditioning HDU - 4756(最小生成树+树形dp)
Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 【HDU - 4340】Capturing a country(树形DP)
BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...
- HDU 5723 Abandoned country(最小生成树 + 树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...
- hdu4126Genghis Khan the Conqueror (最小生成树+树形dp)
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others) Total Submiss ...
随机推荐
- MonkeyRunner_运行脚本(一)
前提:环境已部署, 使用数据线连接上真机 一.使用cmd窗口单步执行monkeyrunner命令 打开cmd窗口,输入monkeyrunner (前提设置好环境变量):然后按照monkeyrunne ...
- elastic search范围查询
queryBuilder.must(QueryBuilders.rangeQuery("pt_longitude").from(minLongitude).to(maxLongit ...
- 遍历出文档内所有元素的tagName
//深度优先 function deepLogTagNames(parentNode){ console.log(parentNode.tagName); const childNodes=paren ...
- tomcat安装apr优化
APR是apache的一个linux操作系统级优化库,可以在tomcat中使用操作系统级native调用大大提高并发处理效率 先安装前置依赖: yum install -y apr-devel ope ...
- hotplug 热拔插机制框架
框架入口源文件: mdev.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6 硬件平台:JZ2440 以下是驱动框架:
- LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- shell日志分析进阶篇
前面我们说了shell分析日志常用指令,现在我们随ytkah一起看看shell日志分析进阶篇,假设日志文件为ytkah.log //统计不重复抓取数量 cat ytkah.log | awk '{pr ...
- ros 运行rviz时出现 QXcbConnection: XCB error: 148 错误 解决方法
出现上述问题的原因: 1.由于使用了nvc远程控制下位机: 2.rviz是一个基于opengl开发的图形插件,需要使用理论的屏幕参数(thetis' screen),由于使用了teamviewer会导 ...
- MSSQL查询收缩和备份进度
--查询当前数据库备份进度 SELECT DB_NAME(er.[database_id]) [DatabaseName],er.[command] AS [CommandType],er.[pe ...
- SQL存储过程分页
CREATE PROC ZDY_FY(@Pages INT, @pageRow INT) --@Pages第几页 @pageRow每页显示几行 AS BEGIN DECLARE @starNum IN ...