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. CentOS7下搭建hadoop2.7.3完全分布式

    这里搭建的是3个节点的完全分布式,即1个nameNode,2个dataNode,分别如下: CentOS-master   nameNode   192.168.11.128 CentOS-node1 ...

  2. Windows 8 快捷键

    三月份,Kurt Walter 在 Windows团队博客中发表了一篇介绍 Windows 8 和全新快捷键的精彩文章. Windows 8 提供了出色的触摸体验,但在我的台式机上使用了几个月后我还发 ...

  3. 高新技术---枚举及部分JDK1.5新特性

    第一讲     枚举 一.概述 这里说的枚举,不是集合vector的特有枚举迭代器,而是JDK1.5的一个新特性.之所以单独拿它开刷,是这个知识点比较重要,同时相对来说比较难理解一些. 为什么要有枚举 ...

  4. PS软件操作应用—文字特效

      前  言 JRedu 在图像调整和文字工具的分享文章中,对文字工具做了简单的介绍,包括了文字的字体.字号大小.颜色以及字间距行距等等的设置和修改,都是一些基本的功能,在这次的分享中我们介绍下文字特 ...

  5. [2016-07-15]结合命令行工具awk和多行文本编辑器快速生成DataSeed代码

    目标:根据业务提供的两份数据,生成DataSeed代码 SampleDataA 上海 华东一线 上饶 华东四线 中山 华南二线 临汾 华北四线 临沂 华东二线 SampleDataB 上海 1D04E ...

  6. 为Markdown文件生成目录

    缘由 思路 效果 代码实现 缘由 Markdown源生文件源生不支持目录,目前比较主流的生成目录的方式(各式插件),都是转化为HTML文件.虽然HTML文件可以生成眼花缭乱的目录,但是修改起来又没有M ...

  7. vue :class的动态绑定

     动态绑定class 写在指令中的值会被视作表达式,如javascript表达式,因此v-bind:class接受三目运算: 1 2 3 4 HTML代码: <div :class=" ...

  8. Tomcat利用Redis存储Session

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt269 如果英文不错的看,建议直接看官网吧,官网写的挺清楚.下面的内容是转载的 ...

  9. linux 增量备份命令Rsync 使用详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt320 Rsync的命令格式可以为以下六种: rsync [OPTION].. ...

  10. Spring 设置readonly 事务只读模式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt394 在学习spring事务的时候,发现设置readOnly后不启作用. 查 ...