题目链接:
n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最小生成树,由于边权各不相同,所以最小生成树唯一。
然后,在这个最小生成树的基础上,求各个路径的最小期望和(推导出 期望 = 所有村庄中任意两个村庄距离之和 / 村庄对数)。
最小生成树很好求(边权从小到大,并查集一下就好了)。
然后求以上基础村庄中任意两个村庄距离之和,只要求每条边乘上每条边出现的次数,每条边出现的次数通过观察看出是u相连点个数乘上v相连点个数。这个dfs一遍就可以求。
最后除以村庄对数就好了。
1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include