poj 2253 Frogger【最小生成树变形】【kruskal】
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 30427 | Accepted: 9806 |
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
题意:给出n个点的坐标,连通第一个点和第二个点有很多条路径,每条路径都会有 一个最大距离d - ->(当前路径经过的点集里面 任意两点间的距离都小于或者等于d)。题目要求找到这样的一个路径:(1)连通1,2两点;(2)这条路径中的最大距离d 是所有可选择路径中最小的。 输出这条路径的最大距离d。 题解:利用并查集kruskal算法;先将任意两个点之间的距离按从小到大的顺序排列,然后开始枚举所有的边,直至找到使起点和终点联通的边;
因为要求所有可以连通的路径的最大权值中最小的,所以一旦找到使其连通的边就是所要求的结果
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 210
#define DD double
using namespace std;
int set[MAX];
DD a[MAX],b[MAX];
int k,t,n;
struct node
{
int u,v;
DD w;
}s[50000];
bool cmp(node a,node b)
{
return a.w<b.w;
}
DD dis(DD x1,DD y1,DD x2,DD y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void init()
{
for(int i=0;i<=n;i++)
set[i]=i;
}
void getmap()
{
for(int i=0;i<n;i++)
scanf("%lf%lf",&a[i],&b[i]);
k=0;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
s[k].u=i;
s[k].v=j;
s[k++].w=dis(a[i],b[i],a[j],b[j]);
}
sort(s,s+k,cmp);
}
int find(int fa)
{
if(fa==set[fa])
return fa;
return set[fa]=find(set[fa]);
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
void solve()
{
int i,j;
int ok;
DD ant;
for(i=0;i<k;i++)
{
ok=0;
ant=s[i].w;
mix(s[i].u,s[i].v);
if(find(0)==find(1))//判断是否连通
ok=1;
if(ok)//第一次连通时就是最小的
{
printf("Scenario #%d\n",++t);
printf("Frog Distance = %.3f\n\n",ant);
return ;
}
}
}
int main()
{
t=0;
while(scanf("%d",&n),n)
{
init();
getmap();
solve();
}
return 0;
}
poj 2253 Frogger【最小生成树变形】【kruskal】的更多相关文章
- 最短路(Floyd_Warshall) POJ 2253 Frogger
题目传送门 /* 最短路:Floyd算法模板题 */ #include <cstdio> #include <iostream> #include <algorithm& ...
- POJ 2253 Frogger ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)
POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- POJ 2253 Frogger(dijkstra 最短路
POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- poj 2253 Frogger 最小瓶颈路(变形的最小生成树 prim算法解决(需要很好的理解prim))
传送门: http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...
- POJ 2253 Frogger
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2253 Frogger (最长路中的最短路)
链接:poj 2253 题意:给出青蛙A,B和若干石头的坐标,现青蛙A想到青蛙B那,A可通过随意石头到达B, 问从A到B多条路径中的最长边中的最短距离 分析:这题是最短路的变形,曾经求的是路径总长的最 ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】
Frogger Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
随机推荐
- angularjs应用骨架(2)
时隔一个星期,接着上一篇的angularjs应用骨架继续聊聊angularjs其他的其他的内容. 区分UI和控制器的职责 在应用控制器中有三种职责: 1.为应用中模型设置初始状态 2.通过$scope ...
- YZOI Easy Round 2_回文串 string
原文链接:http://laphets1.gotoip3.com/?id=18 Description 给出一个由小写字母组成的字符串,其中一些字母被染黑了,用?表示.已知原来的串不是 一个回文串,现 ...
- gridview列前加复选框需要注意的一点
前言 获取gridview每一列前面的复选框,然后获取选中的这一行的id.aspx页面,我不喜欢用这个,有的公司用自己封装的,基本上都是用封装的,这是我知道的.也有用Repeater的.可能是因为gr ...
- Android中通过typeface设置字体
Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使用其他字体文件(*.ttf)方法一:XML中使用android默认字体 ...
- Python 学习笔记(3) - 控制流、函数
控制流语句if.while.for.break.continue以上从最终作用效果来讲,同学过的其他语言没有什么不同.需要注意的只是语法,而Python 在语法上是如此让人赞叹和喜欢啊. 控制流语句的 ...
- ASP.NET MVC 学习
项目结构
- winfrom获得鼠标的坐标
Point mouse = this.PointToScreen(Control.MousePosition);label1.Text = mouse.X.ToString() + ":&q ...
- 关于scroll无法绑定的问题
关于jQuery的scroll([[data],fn])事件, 概述是:当用户滚动指定的元素(元素包括:所有可滚动的元素和 window 对象)时,会触发该事件. 如绑定窗口对象: $(window) ...
- 用了OneAPM CT,宕机早知道!
Twitter 的公司网站和移动应用在 1 月 19 日早上出现宕机,导致全球部分地区用户无法正常访问.这次宕机影响了很多用户,英国和印度用户已经无法访问 Twitter .第三方监测机构 DownD ...
- 如何在DJANGO里,向有外键(一对多和多对多)的DB里插入数据?
需要插入的数据表结构如下: class UserInfo(models.Model): user_id =models.AutoField(primary_key=True) user_name=mo ...