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. Java 关于 == 和 equal()的区别

    因为用new创建了两个对象,所以a和b指向两个不同的内存地址,所以返回false equal()是object的方法,所以只适用于对象,不使用于基本类型.不过equal()默认是用“==”比较两个对象 ...

  2. access数据库密码破解

    根据C语言教学书上的示例编写,主要破解access的密码,通过异或算法,支持access2000和access2003,其他版本的没经过测试,下面是具体代码: #include <stdio.h ...

  3. uva208 - Firetruck

    Firetruck The Center City fire department collaborates with the transportation department to maintai ...

  4. SVN备份批处理文件

    SVN备份批处理文件,亲测可用 另外,备份文件时获取文件名%%~ni 可改为%%~nxi,以避免文件名中有“.”号时,读取不完成,将.后面的当作后缀名 需要使用hotcopy 时,可以将关键代码进行相 ...

  5. NFS错误Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno=Connection refused

    NFS报错一例 [root@bjs0- ~]# /etc/init.d/portreserve start Starting portreserve:                          ...

  6. C# 利用WORD模板和标签(bookmark) 批量生成WORD

    前言: 由于对C#操作WORD不熟悉,也就留下这么一篇水文,别吐糟...=_=||| 利用Microsoft.Office.Interop.Word (2003版也就11版)——因为部分客户端还是用O ...

  7. JVM系列文章(四):类载入机制

    作为一个程序猿,只知道怎么用是远远不够的. 起码,你须要知道为什么能够这么用.即我们所谓底层的东西. 那究竟什么是底层呢?我认为这不能一概而论.以我如今的知识水平而言:对于Web开发人员,TCP/IP ...

  8. 用Systemtap探索MySQL

    http://www.actionsky.com/docs/archives/168#Systemtap 目录 1 Systemtap 2 Systemtap 观测点的支持程度 2.1 官方编译的My ...

  9. Mysql子查询IN中使用LIMIT

    学习下Mysql子查询IN中使用LIMIT的方法. 这两天项目里出了一个问题,mysql LIMIT使用后报错. 需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料 ...

  10. php namespace use 命名空间

    也可以参考PHP官网说明:http://php.net/manual/en/language.namespaces.importing.php namespace(以下简称ns).在定义了一个ns之后 ...