Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28333   Accepted: 9208

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 刚开始不会做,看了网上的提示的后说是这是Dijkstra的变种,然后还风轻云淡的说更新条件变一下就行了,结果这句话坑大了,严格来说这就不是Dijkstra!
Dijkstra维护的是两个集合,加入到S集合之中的点已经确定正确,无需再计算,但是这题不一样,就因为这点WA了十多发。关于原算法中S里的点已经正确的证明移步我前一篇博文,但是对于这题用,同样的证明方法,得不出S里的点已经正确的结论。
首先假设S中的点已经最优,而且假设即将加入S的点u没有最优,那么存在一条路径 S里的点 + 任意一点 -> u 是最优的。
那么,如果此任意点属于S,因为维护小顶堆,所以下一个确实应该讲u加入,此处无矛盾。
然后,如果此任意点不属于S,那么就存在两种情况,要么D[s]>D[u],要么D[s]<D[u],但是两种情况都可以成立,也即是说这条假设的路径是可以存在的!
所以,将算法里对S的集合维护取消就对了,只是这还算迪杰斯特拉么?稍后补上另外几种算法的代码。
 #include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x6fffffff;
int N;
int TEMP[SIZE][];
double D[SIZE];
bool S[SIZE];
struct Comp
{
bool operator ()(int & a,int & b)
{
return D[a] > D[b];
}
};
struct Node
{
int vec;
double cost;
};
vector<Node> G[SIZE]; double dis(int x_1,int y_1,int x_2,int y_2);
void dijkstra(int);
int main(void)
{
int count = ;
Node temp; while(scanf("%d",&N) && N)
{
for(int i = ;i <= N;i ++)
scanf("%d%d",&TEMP[i][],&TEMP[i][]);
for(int i = ;i <= N;i ++)
G[i].clear();
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
temp.vec = j;
temp.cost = dis(TEMP[i][],TEMP[i][],TEMP[j][],TEMP[j][]);
G[i].push_back(temp);
temp.vec = i;
G[j].push_back(temp);
} dijkstra();
printf("Scenario #%d\n",++ count);
printf("Frog Distance = %.3f\n",D[]);
puts("");
} return ;
} double dis(int x_1,int y_1,int x_2,int y_2)
{
return sqrt(pow((double)x_1 - x_2,) + pow((double)y_1 - y_2,));
} void dijkstra(int s)
{
fill(D,D + SIZE,INF);
fill(S,S + SIZE,false);
D[s] = ;
priority_queue<int,vector<int>,Comp> que;
que.push(s); while(!que.empty())
{
int cur = que.top();
que.pop();
if(cur == )
break;
/*S[cur] = true; 注释部分即为原Dij应有的部分,此题要移除,加上即WA*/ for(int i = ;i < G[cur].size();i ++)
if(/*!S[G[cur][i].vec] && */D[G[cur][i].vec] > max(G[cur][i].cost,D[cur]))
{
D[G[cur][i].vec] = max(G[cur][i].cost,D[cur]);
que.push(G[cur][i].vec);
}
}
}
#include <iostream>
#include <queue>
#include <cstdio>
#include <cmath>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
int N;
double D[SIZE];
struct Node
{
int vec;
double cost;
};
struct
{
int x,y;
}TEMP[SIZE];
vector<Node> G[SIZE]; double dis(int,int,int,int);
void SPFA(int);
int main(void)
{
Node temp;
int count = ; while(scanf("%d",&N) && N)
{
for(int i = ;i <= N;i ++)
scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
for(int i = ;i <= N;i ++)
G[i].clear();
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
temp.vec = j;
temp.cost = dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y);
G[i].push_back(temp);
temp.vec = i;
G[j].push_back(temp);
}
SPFA();
printf("Scenario #%d\n",++ count);
printf("Frog Distance = %.3f\n",sqrt(D[]));
puts("");
} return ;
} double dis(int x_1,int y_1,int x_2,int y_2)
{
return pow((double)x_1 - x_2,) + pow((double)y_1 - y_2,);
} void SPFA(int s)
{
queue<int> que;
fill(D,D + SIZE,INF);
D[s] = ;
que.push(s); while(!que.empty())
{
int cur = que.front();
que.pop(); for(int i = ;i < G[cur].size();i ++)
if(D[G[cur][i].vec] > max(D[cur],G[cur][i].cost))
{
D[G[cur][i].vec] = max(D[cur],G[cur][i].cost);
que.push(G[cur][i].vec);
}
}
}

POJ 2253 Frogger (最短路)的更多相关文章

  1. POJ 2253 Frogger 最短路 难度:0

    http://poj.org/problem?id=2253 #include <iostream> #include <queue> #include <cmath&g ...

  2. poj 2253 Frogger(最短路 floyd)

    题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...

  3. POJ 2253 Frogger -- 最短路变形

    这题的坑点在POJ输出double不能用%.lf而要用%.f...真是神坑. 题意:给出一个无向图,求节点1到2之间的最大边的边权的最小值. 算法:Dijkstra 题目每次选择权值最小的边进行延伸访 ...

  4. POJ 2253 Frogger ( 最短路变形 || 最小生成树 )

    题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...

  5. 最短路(Floyd_Warshall) POJ 2253 Frogger

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

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

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

  7. POJ 2253 Frogger(dijkstra 最短路

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

  8. POJ. 2253 Frogger (Dijkstra )

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

  9. poj 2253 Frogger (dijkstra最短路)

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

随机推荐

  1. maven3实战之maven使用入门(使用archetype生成项目骨架)

    maven3实战之maven使用入门(使用archetype生成项目骨架) ---------- maven提供了archetype以帮助我们快速勾勒出项目骨架.以Hello World为例,我们使用 ...

  2. 关于JAVA多线程的那些事__初心者

    前言 其实事情的经过也许会复杂了点,这事还得从两个月前开始说.那天,我果断不干IT支援.那天,我立志要做一个真正的程序猿.那天,我26岁11个月.那天,我开始看Android.那天,我一边叨念着有朋自 ...

  3. [读书笔记]SQL约束

    目的:通过在列级或表级设置约束,确保数据符合某种数据完整性规则 实现:数据库主动地检查维护数据的完整性 手段:约束,数据类型,触发器 --------------------------------- ...

  4. Android vector标签 PathData 画图超详解

    SVG是一种矢量图格式,是Scalable Vector Graphics三个单词的首字母缩写.在xml文件中的标签是<vector>,画出的图形可以像一般的图片资源使用,例子如下: &l ...

  5. Java Zip压缩实现

    最近在自学javaWeb,先复习一下java,把还给老师的东西再找回来(知识如果不用很快就会忘记啊).. 今天看到了zip压缩,决定要整理一下. java将有关zip压缩的内容都封装在java.uti ...

  6. ThinkPHP模板(一)

    如何关闭ThinkPHP的模板缓存 ThinkPHP的模板缓存是无奈关闭的,因为内置的模板引擎是一个编译型的模板引擎,必须经过编译后生成一个可执行的缓存文件才能被执行.但是可以设置缓存的有效期,例如设 ...

  7. Scrum Planning Card

    最近用Swift写了个小工具Scrum Planning Card,如果你也用scrum管理项目, 或许用这个工具可以提高你的工作效率. 另外欢迎提建议和反馈,谢谢. 欢迎关注我的微信公众号 Hope ...

  8. GridView实现多表头合并[转]

    1.这里先介绍单纯的GridView多表头合并,先上图: 可以看到,上图就是生成的多表头,具体的后台代码是在Row_Created事件中创建的.先看创建代码: protected void GridV ...

  9. ARM&Linux 下驱动开发第二节

    驱动文件:qudong.c,make生成qudong.ko文件,烧录到ARM板上 #include<linux/init.h> #include<linux/module.h> ...

  10. 数据结构复习:交换排序原理及C++实现

    1. 交换排序的基本思想 两两比较key值,如果发生逆序(排列的顺序与期望的顺序相反)就交换,知道所有对象都排序完毕!常见的3种交换排序算法:冒泡排序,shaker排序和快速排序. 2. 冒泡排序 设 ...