ACM学习之路___HDU 5723(kruskal + dfs)
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个点,现在要重新修,求出连通每一个点需要的最短路径是多少,并求在连通的路任意选两个点走,它的最小期望。
解法:
#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)的更多相关文章
- ACM学习之路___HDU 1385(带路径保存的 Floyd)
Description These are N cities in Spring country. Between each pair of cities there may be one trans ...
- ACM学习之路___HDU 2066 一个人的旅行
Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...
- ACM学习之路__HDU 1045
Fire Net Description : Suppose that we have a square city with straight streets. A map of a city is ...
- ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)
http://219.244.176.199/JudgeOnline/problem.php?id=1215 这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严 ...
- ACM学习历程—HDU1716 排列2(dfs && set容器)
Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字( ...
- ACM学习之路————一个大整数与一个小整数不得不说得的秘密
这个相对于两个大整数的运算来说,只能说是,low爆了. 只要利用好除法的性质,这类题便迎刃而解.O(∩_∩)O哈哈~ //大整数除一个int数 #include<iostream> #in ...
- ACM学习之路
2018-10-18 11:03:00 今天开始踏上实现梦想的道路,希望自己不要懈怠. 坚持做简单的事,坚持下来就会变得不简单.
- <2014 05 09> Lucida:我的算法学习之路
[转载] 我的算法学习之路 关于 严格来说,本文题目应该是我的数据结构和算法学习之路,但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法(例如算法导论指的实际上是数据结构和算法导论),所以 ...
- acm学习指引
acm学习心得及书籍推荐 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划练练: 第 ...
随机推荐
- linux虚拟机ip地址更改
在虚拟机模式下 进入 cd /etc/sysconfig/network-scripts/ vim ifcfg-eth0 编辑 IPADDR=新的内网ip PREFIX = 24 (对应255.25 ...
- 【SVN】手动删除svn元信息
工作中当重建svn仓库,需要把之前的项目导入到新的仓库中,熟悉又快捷的方式是项目上右键->Team断开连接->删除元信息,然后项目右键->Team>Share Project- ...
- 8.2.1 UML, 组合和聚合、关联和依赖
类A的属性是另一个类B,那么这两个类是关联的,但不一定是聚合,如果在A类中创建了B类的实例(使用new!),那么B类和A类就是聚合关系,但不一定是组合关系,因为不一定在A类创建的同时去创建B类的实例, ...
- 前端UI框架《Angulr》入门
Angulr 项目的名称为 Angulr,对!没错!就是少个 a,少个 a 就是它正确的拼写. 是一个以 Bootstrap 和 AngularJS 为基础,并使用了大量前端开源组件合成的一个前端UI ...
- 【前端】Github Pages 与域名关联简明教程
Github Pages 与域名关联简明教程 1. 向你的 Github Pages 仓库添加一个CNAME(一定要*大写*)文件 其中只能包含一个顶级域名,像这样: example.com 如果你是 ...
- 共享Visio和project的下载链接
好东西就应该共享 下面的是最新版的Visio和project的百度云链接 Visio的链接:http://pan.baidu.com/s/1o8UJq4M 密码:sltu project的链接:ht ...
- github开源项目学习-front-end-collect
About 项目地址 项目预览demo(githubio加载较慢) 开源项目fork自:https://github.com/foru17/front-end-collect 此文章是对此开源项目使用 ...
- 2017最新的Python教程分享
Python在数据科学盛行的今天,其易于阅读和编写的特点,越来越受编程者追捧.在IEEE发布的2017年编程语言排行榜中,Python也高居首位.如果你有学Python的计划,快来看看小编分享的Pyt ...
- webpack常见的配置总结 ---只是一些常见的配置
早期的构建工具grunt ,gulp 帮助我们配置一些开发环境,省去一些我们调试和重复的工作 现在我们的构建工具一般是webpack ,目前建议大家用3.0以上的版本 现在市场上比较优秀的构建工具,个 ...
- ROS学习记录(三)————创建一个简单的发布节点和订阅节点
暑假在家有些懈怠,不,非常懈怠- -||!良心已经发痛了,想快些补回原来的进度,但忽然发现,中断了一段时间再重新去学习,有的地方连最基本的符号都忘记了 ,这次特意弄个最最基础的,恢复一下,以前的进度. ...