Time Limit: 1000MS Memory Limit: 65536K

Description

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping. 
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

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

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

题解:

参考:http://www.cnblogs.com/tanhehe/p/3169865.html

另外请参考:http://blog.csdn.net/PKU_ZZY/article/details/52434239

Dijkstra算法模板:

const int INF=0x3f3f3f3f;
const int maxn=; int n; //n个节点
int d[maxn],edge[maxn][maxn];
bool vis[maxn]; //标记是否在集合S中 /*
集合V:图上所有节点
集合S:已经确定正确计算出d[]的节点
集合Q:V-S
*/
void dijkstra()
{
for(int i=;i<=n;i++) d[i]=(i==)?:INF;
memset(vis,,sizeof(vis)); for(int i=;i<=n;i++)
{
int mini=INF,u;
for(int j=;j<=n;j++)
{
if(!vis[j] && d[j]<mini) mini=d[(u=j)]; //寻找集合Q里d[u]最小的那个点u
}
vis[u]=; //放入集合S for(int v=;v<=n;v++)
{
if(!vis[v]) dist[v]=min(dist[v],dist[u]+edge[u][v]); //对集合Q中,所有与点u相连接的点进行松弛
}
}
}

AC代码:

 #include<cstdio>
#include<cmath>
#define N 205
#define INF 1e60
double max(double a,double b){return a>b?a:b;}
double edge[N][N],d[N];
struct Point{
int x,y;
}point[N];
int n;
bool vis[N];
double dist(Point a,Point b){
return sqrt( (b.y-a.y)*(b.y-a.y) + (b.x-a.x)*(b.x-a.x) );
}
void init()
{
for(int i=;i<=n;i++)
{
if(i==) d[]=0.0 , vis[]=;
else d[i]=dist(point[],point[i]) , vis[i]=; for(int j=i;j<=n;j++)
{
edge[i][j]=edge[j][i]=dist(point[i],point[j]);
}
}
}
double dijkstra()
{
for(int i=;i<=n;i++)
{
double min=INF;int x;
for(int j=;j<=n;j++) if(!vis[j] && d[j] <= min) min=d[(x=j)];//找集合Q( Q=G.V - S )里d[x]最小的那个点x
vis[x]=;//把点x放进集合S里
for(int y=;y<=n;y++)//把在集合Q里所有与点x相邻的点都找出来松弛,因为这里青蛙可以在任意来两石头间跳,所以直接遍历 G.V - S 即可
{
if(!vis[y]){
double tmp = max(d[x], edge[x][y]);
if(d[y] > tmp) d[y] = tmp;
}
}
}
return d[];
}
int main()
{
int kase=;
while(scanf("%d",&n) && n!=)
{
for(int i=;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y);
init();
printf("Scenario #%d\n",++kase);
printf("Frog Distance = %.3f\n\n",dijkstra());
}
}

堆优化的Dijkstra算法:

const int maxn=;
const int INF=0x3f3f3f3f; int n,m; //n个节点,m条边 struct Edge
{
int u,v,w;
Edge(int u,int v,int w){this->u=u,this->v=v,this->w=w;}
};
vector<Edge> E;
vector<int> G[maxn];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void addedge(int u,int v,int w)
{
E.push_back(Edge(u,v,w));
G[u].push_back(E.size()-);
} bool vis[maxn];
int d[maxn]; //标记是否在集合S中
void dijkstra(int st)
{
for(int i=;i<=n;i++) d[i]=(i==st)?:INF;
memset(vis,,sizeof(vis)); priority_queue< pair<int,int> > Q; //此处的Q即集合Q,只不过由于那些d[i]=INF根本不可能被选到,所以就不放到优先队列中
Q.push(make_pair(,st));
while(!Q.empty())
{
int now=Q.top().second; Q.pop(); //选取集合Q中d[x]最小的那个点x
if(vis[now]) continue; //如果节点x已经在集合S中,就直接略过
vis[now]=; //将节点x放到集合S中,代表节点x的d[x]已经计算完毕
for(int i=;i<G[now].size();i++) //松弛从节点x出发的边
{
Edge &e=E[G[now][i]]; int nxt=e.v;
if(vis[nxt]) continue;
if(d[nxt]>d[now]+e.w)
{
d[nxt]=d[now]+e.w;
Q.push(make_pair(-d[nxt],nxt));
}
}
}
}

POJ 2253 - Frogger - [dijkstra求最短路]的更多相关文章

  1. poj 2253 Frogger dijkstra算法实现

    点击打开链接 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21653   Accepted: 7042 D ...

  2. POJ 2253 Frogger (求某两点之间所有路径中最大边的最小值)

    题意:有两只青蛙,a在第一个石头,b在第二个石头,a要到b那里去,每种a到b的路径中都有最大边,求所有这些最大边的最小值.思路:将所有边长存起来,排好序后,二分枚举答案. 时间复杂度比较高,344ms ...

  3. POJ - 2253 Frogger 单源最短路

    题意:给定n个点的坐标,问从第一个点到第二个点的最小跳跃范围.d(i)表示从第一个点到达第i个点的最小跳跃范围. AC代码 #include <cstdio> #include <c ...

  4. POJ 2253 Frogger(dijkstra 最短路

    POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...

  5. POJ. 2253 Frogger (Dijkstra )

    POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...

  6. 最短路(Floyd_Warshall) POJ 2253 Frogger

    题目传送门 /* 最短路:Floyd算法模板题 */ #include <cstdio> #include <iostream> #include <algorithm& ...

  7. POJ 2253 Frogger ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)

    POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...

  8. POJ 2253 Frogger (dijkstra 最大边最小)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description The i ...

  9. 关于dijkstra求最短路(模板)

    嗯....   dijkstra是求最短路的一种算法(废话,思维含量较低,   并且时间复杂度较为稳定,为O(n^2),   但是注意:!!!!         不能处理边权为负的情况(但SPFA可以 ...

随机推荐

  1. Spring中的类型转换与数据绑定(PropertyEditor、ConversionService、Data Binding、Formatter)

    Spring早期使用PropertyEditor进行Object与String的转换.到Spring 3后,Spring提供了统一的ConversionService API和强类型的Converte ...

  2. scala spray 概念性内容的总结

    spray 是基于 akka 的轻量级 scala 库,可用于编写 REST API 服务.了解 spray 的 DSL 后可以在很短的时间内写出一个 REST API 服务,它的部署并不需要 tom ...

  3. 18个不常见的C#关键字,您使用过几个?

    转自:http://www.cnblogs.com/zhuqil/archive/2010/04/09/UnCommon-Csharp-keywords-A-Look.html 1.__arglist ...

  4. ASP.NET MVC 4 (二)控制器

    MVC中控制器负责处理请求,由它操作数据模型,最后返回视图给用户. IController接口 所有的控制器类以Controller结尾,必须实现System.Web.Mvc.IController接 ...

  5. C#反射基础理解1(转)

    反射提供了封装程序集.模块和类型的对象(Type类型) 可以使用反射动态的创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后,可以调用类型的方法或访问其字段和属性 . 总之,有了反射, ...

  6. 为什么GPL是更好的开源许可证?

    1. 让我从一件新闻讲起. 2009年,计算机业界发生了一件大事:甲骨文公司以74亿美元收购SUN公司. 消息宣布后,有一个人坚决反对这笔交易.他叫Michael Widenius,是数据库软件MyS ...

  7. redis 列表

    Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967 ...

  8. vs 2010中如何检查内存泄露

    首先,在文件头添加下面的内容: #ifdef _DEBUG#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)#else# ...

  9. c++学习笔记—二叉树基本操作的实现

    用c++语言实现的二叉树基本操作,包括二叉树的创建.二叉树的遍历(包括前序.中序.后序递归和非递归算法).求二叉树高度,计数叶子节点数.计数度为1的节点数等基本操作. IDE:vs2013 具体实现代 ...

  10. window下线程同步之(Mutex(互斥器) )

    使用方法: 1.创建一个互斥器:CreateMutex: 2.打开一个已经存在的互斥器:OpenMutex: 3.获得互斥器的拥有权:WaitForSingleObject.WaitForMultip ...