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. c# 定义委托和使用委托(事件的使用)

    使用委托时要先实例化,和类一样,使用new关键字产生委托的新实例,然后将一个或者多个与委托签名匹配的方法与委托实例关联.随后调用委托时,就会调用所有与委托实例关联的方法. 与委托关联可以是任何类或者结 ...

  2. 根据IP获取所在的国家城市

    根据IP获取所在的国家城市 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:htt ...

  3. tornado 初识

    Tornado 是一个Python Web框架和异步网络库,最初是在FriendFeed上开发的. 通过使用非阻塞网络I / O,Tornado可以扩展到数万个开放连接,使其成为长轮询, WebSoc ...

  4. Codeforce 296A - Yaroslav and Permutations

    Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...

  5. 018.07 New BMW ICOM A3+B+C+D Plus EVG7 Controller Tablet PC with WIFI Function

    2018.07 New BMW ICOM A3+B+C+D Plus EVG7  Controller Tablet PC with WIFI Function Software Version : ...

  6. Java开发学习教程之对象的创建与使用

    java面向对象中的对象创建与使用.类是对象的抽象,为对象定义了属性和行为,但类本身既不带任何数据,也不存在于内存空间中.而对象是类的一个具体存在,既拥有独立的内存空间,也存在独特的属性和行为,属性还 ...

  7. checkbox 全选效果

    html部分 <p id="all">全选</p> <input type="checkbox" /><br/> ...

  8. Java Web-----JSP与Servlet(一)

    JSP (java服务器页面) JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1]  是由Sun Microsystems公司倡 ...

  9. 我仅使用到的dd if

    备份一个分区 分区 镜像名 读写块大小 dd if=/dev/sdb of=/diskone.img bs=512 注:可以为了提升I/O把bs设为较高的数值例:bs=1024k 挂载一个分区 mou ...

  10. python简说(十二)time模块

    1.时间戳 print(int(time.time())) 2.取当前格式化好的时间 time.strftime('%Y-%m-%d %H:%M:%S') 3.时间戳转为格式化好的时间 time1 = ...