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上. 下面给个计划练练: 第 ...
随机推荐
- VS2015 安装nuget离线包nupkg文件
最近在做项目进度管理时,想通过安装net.sf.mpxj-for-csharp包读取.mpp格式文件,通过Nuget在线安装时,出现以下情况,无法安装,故开启离线安装道路. 离线安装步骤如下: 一.下 ...
- 一个页面多个iframe赋值
先在页面上设置一个元素: <input type="hidden" id="content" value={$content}> 使用前端技术获取父 ...
- 数据结构-环形队列 C和C++的实现
队列: 含义:是一种先入先出(FIFO)的数据结构. 当我们把数据一个一个放入队列中.当我们需要用到这些数据时,每次都从队列的头部取出第一个数据进行处理.就像排队进场一样,先排队的人先进场. 结构如下 ...
- shell之参数传递
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推-- 实例 以下实例我们向脚本传递三 ...
- C++中const关键字用法
为什么使用const?采用符号常量写出的代码更容易维护:指针常常是边读边移动,而不是边写边移动:许多函数参数是只读不写的.const最常见用途是作为数组的界和switch分情况标号(也可以用枚举符代替 ...
- localStorage sessionStorage 和cookie等前端存储方式总结
localStorage sessionStorage 和cookie localStorage localStorage是本地存储的,除非清空本地数据 localStorage不会自动把数据发给服务 ...
- Mysql 分区详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt120 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.m ...
- log4j与log4j.properties的配置
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt233 摘要: 一.配置步骤 1. 在应用程序中使用log4j 2. 把l ...
- Linux-mount命令和umount命令 (8)
mount:用于挂载文件系统,使能访问其它文件系统中的资源 umount:用于卸载已挂载的文件系统 mount: 格式: mount [-参数] [设备名称] [挂载点] 其中常用的参数(参数默认自带 ...
- poj 1948二维01背包
题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积. 思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600 ...