Abandoned country

Time Limit: / MS (Java/Others)    Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): Problem Description
An abandoned country has n(n≤) villages which are numbered from to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤) roads to be re-built, the length of each road is wi(wi≤). 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. Input
The first line contains an integer T(T≤) 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. Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space. Sample Input Sample Output
3.33

Problem Description

题意 :

  国家有M条废弃的路和N个点,现在要重新修,求出连通每一个点需要的最短路径是多少,并求在连通的路任意选两个点走,它的最小期望。

解法:

  最短路径好求,单纯最小生成树。但是第一眼看见求期望,MD,啥几把题,很难懂,这都过了这么多天,重新捡起这道题,发现网上都是 最小生成树+dfs 纠结了好久,确实不知道dfs究竟怎么求得期望,看了网上一片博文,大致知道了期望怎么求,但是看懂 dfs 还
是花费了好长时间(看懂之后一直觉得这什么傻逼题)。
 
具体求每条路的贡献 :
  设某一段路在用DFS遍历路径过程中在该条路径遍历过的次数为 cnt,设该条路length ,则它的贡献就是 cnt * ( nodeNum - cnt ) * length。
 
  最后题目中所求期望就是所有的路的贡献加起来  除以(nodeNum * ( nodeNum - 1.0 ) / 2.0 ) 。
开始参照网上的代码交了一发 内存有4W K+ , 后来按照以前kruskal 算法的模板(毕竟我还是太水)加上自己一些想法优化些,时间没怎么减少,内存可以降到 2W K+ , 上一发我的代码 
 #include <bits/stdc++.h>
#define maxn 100005
using namespace std;
double res; vector< pair<int,int> >Edge1[maxn]; struct Edge
{
int from;
int to;
int length;
friend bool operator < (const Edge &e1 , const Edge &e2)
{
return e1.length > e2.length;//最小值优先
}
}; int father[maxn]; //用来做并查集
int nodeNum,edgeNum; //顶点数、边数
long long MST; //最小生成树边权值和 priority_queue<Edge> myQ; //优先队列 void storeMap() //存储岛的桥构成的图
{
while(!myQ.empty())
myQ.pop(); //清空队列
int from,to,length;
for(int i = ; i < edgeNum ; i++) //kruskal算法对于无向图也只需建一条边即可
{
scanf("%d%d%d",&from,&to,&length);
Edge e;
e.from = from;
e.to = to;
e.length = length;
myQ.push(e);
}
} int findx(int x) //查找父节点
{
if(x == father[x])
return father[x];
return father[x] = findx(father[x]);
} bool judge() //判断是否是一棵最小生成树 ,这里得注意起点和终点
{
int f = findx();
for(int i = ; i <= nodeNum ; i++)
{
if(f != findx(i))
return false;
}
return true;
} void init()//初始化函数
{
for(int i = ; i <= nodeNum ; i++)
{
father[i] = i;
Edge1[i].clear();
}
return;
}//特意把 maxn 改成 nodeNum 并且把这个模块从底下的函数中独立出来,没想到时间一点也没少,反倒增加了,很是迷茫 int kruskal() //kruskal算法
{
MST = ;
int num = ; //记录MST的边数
while(!myQ.empty() && num != nodeNum-)
{
Edge e = myQ.top();
myQ.pop();
int fx = findx(e.from);
int fy = findx(e.to);
if(fx != fy)
{
father[fx] = fy;
MST += e.length;
Edge1[ e.from ].push_back(make_pair(e.to , e.length));
Edge1[ e.to ].push_back(make_pair(e.from , e.length));
num++;
}
}
return num;
} int dfs(int x , int f)
{
int cnt = ;//该条路遍历过次数
for(int i = ; i < Edge1[x].size() ; i++)
{
int v = Edge1[x][i].first;
if(v == f)
continue;
int fcnt = dfs( v , x );
cnt += fcnt;
res = res + 1.0 * fcnt * ( nodeNum - fcnt) * Edge1[x][i].second;//该条路的贡献
}
return cnt + ;
} int main()
{
//freopen("sample.in" , "r" , stdin);
//freopen("sample1.out" , "w" , stdout);
int t;
scanf("%d",&t);
while(t--)
{
res = ;
scanf("%d%d",&nodeNum , &edgeNum);
storeMap();
init();
kruskal();
dfs( , );
res = res * 2.0 / (nodeNum *1.0) /(nodeNum - 1.0);
printf("%I64d %.2lf\n",MST,res );
}
return ;
}

ACM学习之路___HDU 5723(kruskal + dfs)的更多相关文章

  1. ACM学习之路___HDU 1385(带路径保存的 Floyd)

    Description These are N cities in Spring country. Between each pair of cities there may be one trans ...

  2. ACM学习之路___HDU 2066 一个人的旅行

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  3. ACM学习之路__HDU 1045

    Fire Net Description : Suppose that we have a square city with straight streets. A map of a city is ...

  4. ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)

    http://219.244.176.199/JudgeOnline/problem.php?id=1215 这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严 ...

  5. ACM学习历程—HDU1716 排列2(dfs && set容器)

    Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数.   Input 每组数据占一行,代表四张卡片上的数字( ...

  6. ACM学习之路————一个大整数与一个小整数不得不说得的秘密

    这个相对于两个大整数的运算来说,只能说是,low爆了. 只要利用好除法的性质,这类题便迎刃而解.O(∩_∩)O哈哈~ //大整数除一个int数 #include<iostream> #in ...

  7. ACM学习之路

     2018-10-18 11:03:00 今天开始踏上实现梦想的道路,希望自己不要懈怠. 坚持做简单的事,坚持下来就会变得不简单.

  8. <2014 05 09> Lucida:我的算法学习之路

    [转载] 我的算法学习之路 关于 严格来说,本文题目应该是我的数据结构和算法学习之路,但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法(例如算法导论指的实际上是数据结构和算法导论),所以 ...

  9. acm学习指引

    acm学习心得及书籍推荐   一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划练练: 第 ...

随机推荐

  1. 【踩坑】angularJS 1.X版本中 ng-bind 指令多空格展示

    做项目的时候遇到的问题 1.问题描述 用户在表单某个值输入多个空格,例如:A     B,保存至服务器 在列表查询页面中使用bg-bind的指令单向绑定,结果展示位A B,连续的空格被替换为单个空格 ...

  2. Amazon Aurora解读(SIGMOD 2017)

    Amazon在SIGMOD 2017发表了论文<Amazon Aurora: DesignConsiderations for High Throughput Cloud-Native Rela ...

  3. 好用的sql

    @ 复制表结构 ; --复制表结构和数据 create table table_name_new as select * from <table_name> @ 查看表信息 select ...

  4. 自己动手写java 字节流输入输出流

    数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流.   "流是磁盘或其它外围设备中存储的数据的源点或终点." ...

  5. Keil报错failed to execute 'd:\Keil\C51\BIN\C51.EXE'

    关于老师发的keil软件报错如下: --- Error: failed to execute 'd:\Keil\C51\BIN\C51.EXE' 错误是因为老师直接拷贝的安装目录,里面的文件路径设置仍 ...

  6. 微软微服务eShopOnContainers示例之EventBusRabbitMq解析与实践

    eShopOnContainers eShopOnContainers是微软官方的微服务架构示例,GitHub地址https://github.com/dotnet-architecture/eSho ...

  7. Membership ——入门基础

    Membership是ASP.NET为我们提供的一套验证和存储用户凭证的框架.它可以帮助我们快速的开发用户登录.管理以及权限验证相关的模块,很多人可能会说Membership开发起来确实很快,但是总是 ...

  8. 误删libc.os.6共享库的解决办法

    在我们使用系统的过程中,要注意各个共享库的使用,万一不小心删掉了什么,就可能出现各种问题.如果你把libc.os.6删掉了,那可就悲剧了,因为你的大部分命令都不能够正常使用了(╥╯^╰╥) 接下来呢, ...

  9. 第二次项目冲刺(Beta阶段)5.20

    1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况,心得分享以及困难分析. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #42文件分类改 ...

  10. 团队作业八——第二次团队冲刺(Beta版本)第6天

    团队作业八--第二次团队冲刺(Beta版本)第6天 一.每个人的工作 (1) 昨天已完成的工作 简单模式逻辑代码涉及与相关功能的具体实现 (2) 今天计划完成的工作 修改完善注册登录内容界面,编辑错题 ...