http://acm.split.hdu.edu.cn/showproblem.php?pid=5723

题意:
给出一个无向图,每条路都有一个代价,求出把所有城市连通的最小代价。在此基础上,国王会从这里面随机挑出两个城市作为起点和终点,求出国王要走的路的期望值。

思路:

第一问很简单,最小生成树计算一下即可。

对于第二问,在新图的基础上,考虑每条边所能给出的贡献值。假设这条边两个的点数分别为x和y,那么这条边总的贡献次数就是x*y,贡献值为x*y*w。求两边的点数dfs即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, m;
double tot; struct node
{
int u,v,w;
bool operator< (const node& rhs) const
{
return w<rhs.w;
}
}edge[]; int p[maxn];
vector<pll> G[maxn]; int Find(int x)
{
return p[x]==x?x:p[x]=Find(p[x]);
} void Kruskal()
{
ll sum = ;
int num=;
sort(edge,edge+m);
for(int i=;i<m;i++)
{
int x = Find(edge[i].u);
int y = Find(edge[i].v);
if(x!=y)
{
p[x]=y;
sum+=edge[i].w;
num++;
G[edge[i].u].push_back(make_pair(edge[i].v,edge[i].w));
G[edge[i].v].push_back(make_pair(edge[i].u,edge[i].w));
}
if(num==n-) break;
}
printf("%I64d ",sum);
} int dfs(int u, int fa)
{
int cnt = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].first;
int w = G[u][i].second;
if(v==fa) continue;
int now = dfs(v, u);
cnt += now;
tot += 1.0 * now * (n - now) * w;
}
return cnt+;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {p[i]=i;G[i].clear();}
for(int i=;i<m;i++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
Kruskal();
tot=;
dfs(,-);
printf("%.2f\n",tot*2.0/(1.0*n)/(n-1.0));
}
return ;
}

HDU 5723 Abandoned country(最小生成树+边两边点数)的更多相关文章

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

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

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

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

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

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

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

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

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

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

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

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

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

  9. hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)

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

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

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

随机推荐

  1. 证券化代币的时代已经到来,STO将引爆区块链经济

    STOs 似乎会在 2019 年取代 ICOs,即使不是完全取代,但置换的比例也会相当大.所有在美上市的公司都将按照 SEC 制定的相关规定进行交易.Vellum Capital 的 CEO 兼管理合 ...

  2. python GIL 全局锁,多核cpu下的多线程性能究竟如何?

    python GIL 全局锁,多核cpu下的多线程性能究竟如何?GIL全称Global Interpreter Lock GIL是什么? 首先需要明确的一点是GIL并不是Python的特性,它是在实现 ...

  3. [转载]web安全之token

    参考:http://blog.csdn.net/sum_rain/article/details/37085771 Token,就是令牌,最大的特点就是随机性,不可预测.一般黑客或软件无法猜测出来. ...

  4. c#测试执行时间的方法

    获取当前实例测量出来的总的运行时间 Stopwatch sp = new Stopwatch(); sp.Start(); //要测试的代码块 sp.Stop(); Console.WriteLine ...

  5. JSP Servlet javaben

    CLASSPATH=D:\Dev\jdk\lib\tools.jar;D:\Dev\jdk\lib\dt.jarJAVA_HOME=D:\Dev\jdkPath=%JAVA_HOME%\bin 一:T ...

  6. url去重 --布隆过滤器 bloom filter原理及python实现

    https://blog.csdn.net/a1368783069/article/details/52137417 # -*- encoding: utf-8 -*- ""&qu ...

  7. sqlalchemy 小试

    # -*- coding: utf-8 -*- from sqlalchemy import Column, String, create_engine,ForeignKey,Text,INTEGER ...

  8. Python求最大可能

    也称为求一个集合的所有的子集 采用二进制方法: def PowerSetsBinary(items): #generate all combination of N items N = len(ite ...

  9. JS方法转字符串

    今天接手的代码比较特殊,需要动态拼接一个table,每一行<tr>都是通过转换为字符串,再拼接在一起放到tbody中的. 其中有的td标签中有a标签,需要给a标签添加点击事件,参数好多,动 ...

  10. Python3 hasattr()、getattr()、setattr()函数简介

    Python3 hasattr().getattr().setattr()函数简介 一.hasattr(object, name) 判断object对象中是否存在name属性,当然对于python的对 ...