HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国)
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Description |
题目描述 |
An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guaranteed that any two wi are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk. |
一个落后渣国有n(n≤100000)座从1到n编号的村子。荒废久了,路也该重新修修。有m(m≤1000000)条路需要重建,每条路长wi(wi≤1000000)。每条路长wi各不相同。旧时的路使村子直接或间接连通。每条路重修的单位造价相同。国王希望花最少的钱使各个村子直接或间接连通。待到这些路重修好后,国王会物色一个信使。国王会等概率地选择两个点作为起点和终点。现在国王想问你这个工程的最低造价与信使所走过路程的期望。 |
Input |
输入 |
The first line contains an integer T(T≤10) which indicates the number of test cases. For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi. |
第一行是一个整数T(T≤10)表示测试用例的数量。 对于每个测试用例,第一行有两个整数n,m表示村子与待重修路的数量。接下来m行,每行有三个数i,j,wi,表示有一条长wi的路连接村子i与j。 |
Output |
输出 |
output the minimum cost and minimum Expectations with two decimal places. They separated by a space. |
输出最低造价与保留两位小数的最小期望。用一个空格分开。 |
Sample Input - 输入样例 |
Sample Output - 输出样例 |
1 |
6 3.33 |
【题解】
最小生成树+求期望,注意越界。
期望 = 总路程/方法数。
总路程分散到每条路上求,每条路经过的距离=长度*上方节点数*下方节点数。DFS可轻松求下方节点数,因此,上方节点数=n-下方节点数。
方法数=1+2+……+(n-1),即n*(n-1)/2。
【代码 C++】
#include <cstdio>
#include <cstring>
#include <algorithm>
int n, m, link[], head[], iE;
__int64 fCost, sum, cnt;
struct Edge{
int to, next, len;
}edge[];
void addEdge(int u, int v, int len){
edge[iE].to = v; edge[iE].len = len; edge[iE].next = head[u];
head[u] = iE++;
}
struct Road{
int a, b, len;
bool operator<(const Road &r) const{
return len < r.len;
}
}iRoad[];
int fid(int a){
return link[a] == a ? a : link[a] = fid(link[a]);
}
bool beCnct(int a, int b){
if (a == b) return ;
return link[a] = b;
}
void build(){
int i, j, u, v, len;
for (i = ; i <= n; ++i) link[i] = i;
fCost = iE = ; memset(head, -, sizeof(head)); for (i = ; i < m; ++i) scanf("%d%d%d", &iRoad[i].a, &iRoad[i].b, &iRoad[i].len);
std::sort(iRoad, iRoad + m); for (i = , j = ; j < n; ++i){
u = iRoad[i].a; v = iRoad[i].b; len = iRoad[i].len;
if (beCnct(fid(u), fid(v))){
addEdge(u, v, len); addEdge(v, u, len);
fCost += len; ++j;
}
}
cnt = (__int64)n*(n - ) >> ;
} __int64 DFS(int now, int pre){
int i;
__int64 opt = , temp;
for (i = head[now]; ~i; i = edge[i].next){
if (i == pre) continue;
opt += temp = DFS(edge[i].to, i ^ );
sum += temp*(n - temp)*edge[i].len;
}
return opt;
}
int main(){
int t;
scanf("%d", &t);
while (t--){
scanf("%d%d", &n, &m);
build();
sum = ; DFS(, -);
printf("%I64d %.2lf\n", fCost, 1.0*sum / cnt);
}
return ;
}
HDU 5723 Abandoned country(落后渣国)的更多相关文章
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
- hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
随机推荐
- redis 笔记06 发布与订阅、事务、慢查询日志、监视器
发布与订阅 1. 服务器状态在pubsub_channels字典保存了所有频道的订阅关系:SUBSCRIBE命令负责将客户端和被订阅的频道关联到这个字典里面,而UNSUBSCRIBE命令则负责 解除客 ...
- 记一个由MemCached引发的性能问题
最近有个项目用loadrunner做了压力测试,发现并发量还不到200服务器就支撑不住了.boss那边紧急开会,说此项目最近3个月内将有100家中大型公司用于校园招聘工作,如果这个问题不解决公司有可能 ...
- 通过SQL Server Profiler来监视分析死锁
在两个或多个SQL Server进程中,每一个进程锁定了其他进程试图锁定的资源,就会出现死锁,例如,进程process1对table1持有1个排它锁(X),同时process1对table2请求1个排 ...
- HTTP Live Streaming直播(iOS直播)技术分析与实现
本文转载自:http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html 不经意间发现,大半年没写博客了,自觉汗颜.实则2012后半 ...
- Python源代码目录组织结构
- C++ 关键字 explicit, export, mutable
转自 explicit 如果A类有某个构造函数的单个输入参数,是B类(包括基本数据类型)的对象或引用,则C++的编译器会在需要A类形参的函数调用中,自动调用该构造函数,将B类实参隐式地转换为A类实参. ...
- [ios]app后台运行
参考:http://www.douban.com/note/375127736/ 1 使用开源代码MMPDeepSleepPreventer将文件加入工程,包括音频文件.可以在源文件中加入单例,便于使 ...
- 错误Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat
今天算是见识了什么事顺序的重要性. 在使用mybatis时由于联合了其他的表,用到了resultMap,之后外加association这一项.可是在替换对应字段的位置上加上association总是报 ...
- Djnago的一些零碎知识点
1.TEMPLATE_DIRS relative to the project folder http://stackoverflow.com/questions/9856683/using-pyth ...
- PHP获取手机相关信息
该PHP操作类实现获取手机号手机头信息,取UA,取得手机类型,判断是否是opera,判断是否是m3gate,取得HA,取得手机IP 代码如下: <?php /** * @desc 手机操作类 获 ...