poj-2253(最小瓶颈路问题)
Description
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
Output
Sample Input
2
0 0
3 4 3
17 4
19 4
18 5 0
Sample Output
Scenario #1
Frog Distance = 5.000 Scenario #2
Frog Distance = 1.414
Source
可用二分法与BFS法解决,但有更好的算法。先求出最小生成树,起点和终点在树上的唯一路径就是我们所要找的路径。需要注意的是这道题只用求最短的那条边,所以如果用kruskal算法不用将最小生成树求出来,只要起点和终点连通即可。
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath> using namespace std; struct aa{
int x;
int y;
aa(int x1,int y1):x(x1),y(y1){}
}; struct bb{
int a;
int b;
int c;
bb(int a1,int b1,int c1):a(a1),b(b1),c(c1){}
bool operator < (const bb &rhs)const{
return c > rhs.c;
}
}; vector<aa>s;
priority_queue<bb>z;
int p[]; int find_z(int x){
return p[x]==x?x:p[x]=find_z(p[x]);
} int main(){
int n;
int yy=;
while(~scanf("%d",&n)){
if(n==)break;
yy++;
s.clear();
while(!z.empty())z.pop(); for(int i=,q,w;i<n;i++){
scanf("%d%d",&q,&w);
s.push_back(aa(q,w));
} for(int i=,o;i<n;i++){
for(int j=;j<n;j++){
o=(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y);
z.push(bb(i,j,o));
}
} for(int i=;i<n;i++)p[i]=i;
while(!z.empty()){
bb zz=z.top(); z.pop();
if( find_z(zz.a) != find_z(zz.b) ){
p[find_z(zz.a)]=find_z(zz.b);
}
if(find_z()==find_z()){
printf("Scenario #%d\n",yy);
double sss=(double)zz.c;
printf("Frog Distance = %.3lf\n\n",sqrt(sss));
break;
}
}
}
return ;
}
poj-2253(最小瓶颈路问题)的更多相关文章
- 最小瓶颈路 Uva 534 Frogger
说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做. 最小瓶颈路:找u到v的一条路径满足最大边权值尽量小 先求最小生成树,然后u到v的路径在树上是唯一的,答案就是这 ...
- UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)
题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...
- UVA 11354 Bond(最小瓶颈路+倍增)
题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...
- 【UVA534】Frogger 最小瓶颈路
题目大意:给定一张 N 个点的完全图,求 1,2 号节点之间的一条最小瓶颈路. 题解:可知,最小瓶颈路一定存在于最小生成树(最小瓶颈树)中.因此,直接跑克鲁斯卡尔算法,当 1,2 号节点在同一个联通块 ...
- 【20181102T2】飞越行星带【智商题+最小瓶颈路】
题面 [正解] 一眼不可做啊 --相当于求路线上穿过的点最小距离最大 最小最大--二分啊 现在相当于给一个直径,要判断这个直径是否能从左边穿到右边 我们可以在距离不超过直径的点连一条边,\(y=0\) ...
- UVa 11354 邦德(最小瓶颈路+LCA)
https://vjudge.net/problem/UVA-11354 题意: 有n个城市m条道路,每条道路有一个危险系数.先在有若干个询问,要求找到一条从s到t的路,使得途径所有边的最大危险系数最 ...
- 【UVA10816】Travel in Desert (最小瓶颈路+最短路)
UVA10816 Travel in Desert 题目大意 沙漠中有一些道路,每个道路有一个温度和距离,要求s,t两点间的一条路径,满足温度最大值最小,并且长度最短 输入格式 输入包含多组数据. 每 ...
- HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- P1396 营救(最小瓶颈路)
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- LOJ#137. 最小瓶颈路 加强版(Kruskal重构树 rmq求LCA)
题意 三倍经验哇咔咔 #137. 最小瓶颈路 加强版 #6021. 「from CommonAnts」寻找 LCR #136. 最小瓶颈路 Sol 首先可以证明,两点之间边权最大值最小的路径一定是在最 ...
随机推荐
- webstorm 添加代码模板
file>setting>Live Templates>选择文件类型
- 将short类型转换为char类型
void short2char(uint8_t *arr1, uint16_t *arr2, int len) { ; ; for (x; x < len; x++) { arr1[x++] = ...
- HDOJ1384 Intervals 题解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1384 大意:有 \(n\) 个区间 \([a_i,b_i]\),每个区间有个权值 \(c_i\),找到 ...
- demo 基于html css 实现小米官网部分内容搭建
文件夹目录 mi-site-----------主文件夹 css------------css文件文件夹 fonts.css---矢量图标css index.css---主页面样式css reset. ...
- UVA1349(带权二分图最大匹配 --> KM算法模板)
UVA1349 题意:给定一些有向带权边,求出把这些边构造成一个个环,总权值最小 解法: 对于带权的二分图的匹配问题可以用通过KM算法求解. 要求最大权匹配就是初始化g[i][j]为0,直接跑就可以: ...
- Python 用户输入&while循环 初学者笔记
input() 获取用户输入(获取的都是字符串哦) //函数input()让程序停止运行,等待用户输入一些文本. //不同于C的是可在input中添加用户提示,而scanf不具备这一特性. //提示超 ...
- P4883 mzf的考验[平衡树]
P4883 mzf的考验 维护一种数据结构 支持区间翻转 区间异或 区间求和- 显然 fhq treap 区间异或显然是拆位 ~~然后复杂度*20~~ 第一次先遍历一下整棵树 pushup 一下 就可 ...
- Verilog-case、casez和casex的区别
参考博客:https://www.cnblogs.com/guolongnv/articles/6906929.html 1.基本概念 1)?表示z,而不是“dont care” 2)区分: case ...
- java使用JDBC连接hive(使用beeline与hiveserver2)
首先虚拟机上已经安装好hive. 下面是连接hive需要的操作. 一.配置. 1.查找虚拟机的ip 输入 ifconfig 2.配置文件 (1)配置hadoop目录下的core-site.xml和hd ...
- PP: Meta-learning framework with applications to zero-shot time-series forecasting
From: Yoshua Bengio Problem: time series forecasting. Supplementary knowledge: 1. what is meta-learn ...