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 ...
随机推荐
- eclipse EE neon创建dynamic web project时,卡在installing dynamic web module facet,解决办法
我们在用eclipse EE neon创建dynamic web project时,如果你发现底部状态栏一直卡在installing dynamic web module facet,永远到不了100 ...
- hdu 1396 Counting Triangles(递推)
Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...
- Eclipse debug调试
Eclipse debug调试: F5:跳入方法F6:向下逐行调试F7:跳出方法F8:直接跳转到下一个断点
- JqGrid实现自定义查询
$("#jqGridId").setGridParam({url:"数据查询地址"}).trigger("reloadGrid");
- jQuery动态添加的元素中处理字符串溢出后在指定字符数后添加省略号
"+[jsonData[i].questitle.lenth>40?jsonData[i].questitle.substring(0,40)+"...":json ...
- npm install express -g出错
npm ERR! Windows_NT npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program ...
- Cinder-1 TinderBox
Cinder:http://libcinder.org/,当前版本是0.8.5,代码托管位置:https://github.com/cinder/Cinder.git 下载Cinder之后,其目录结构 ...
- GMM+Kalman Filter+Blob 目标跟踪
转 http://www.cnblogs.com/YangQiaoblog/p/5462453.html ==========图片版================================== ...
- jQuery组件写法
知识点: 什么是插件 jQuery插件的模式 jQuery插件的Lightweight Start模式(入门级插件模式) 8.1 插件(Plug-in) “插件”这个关键字,估计大家在日常生活中经常有 ...
- bootcamp
为了鄙社自主研发的html5studio和mist,我给Air划了32G装windows囧 第一要注意的是,必须使用bootcamp划分将要安装windows的分区,不要在windows安装过程中删除 ...