POJ 2253 Frogger (最短路)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 28333 | Accepted: 9208 |
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 刚开始不会做,看了网上的提示的后说是这是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 (最短路)的更多相关文章
- POJ 2253 Frogger 最短路 难度:0
http://poj.org/problem?id=2253 #include <iostream> #include <queue> #include <cmath&g ...
- poj 2253 Frogger(最短路 floyd)
题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...
- POJ 2253 Frogger -- 最短路变形
这题的坑点在POJ输出double不能用%.lf而要用%.f...真是神坑. 题意:给出一个无向图,求节点1到2之间的最大边的边权的最小值. 算法:Dijkstra 题目每次选择权值最小的边进行延伸访 ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 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 Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
随机推荐
- 结构类模式(四):装饰(Decorator)
定义 动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 特点 装饰对象和真实对象有相同的接口.这样客户端对象就能以 ...
- Tomcat修改网址旁边的小图标
用Tomcat发布时候,往往不希望用IP地址旁边的Tomcat小猫,根据需求利用自己的icon图标. 先将需要的图片转换成icon格式的. 这个网站可以进行icon格式的转换:http://www.c ...
- UI:target-action设计模式、手势识别器
⼀.target/action设计模式 ⼆.代理设计模式 三.UIImageView 四.⼿势识别器 target/action设计模式 耦合是衡量⼀个程序写的好坏的标准之⼀, 耦合是衡量模块与模块之 ...
- Javascript模块规范(CommonJS规范&&AMD规范)
Javascript模块化编程(AMD&CommonJS) 前端模块化开发的价值:https://github.com/seajs/seajs/issues/547 模块的写法 查看 AMD规 ...
- perl学习笔记(2)
1)记得刚开始写perl的时候,对于一个功能,总是拿目前能用的数据类型来解决问题,不想想有没有更好的,能用能解决问题就好,这就导致了后期,要在函数里面添加功能的时候,函数要添加很多参数,一个函数有7. ...
- OSX apache vhost 配置多站点时403错误解决方法
到 /etc/apache2/httpd.conf 这个文件修改下面的路径就好了 DocumentRoot "/Users/wujinhang/workspace/"<Dir ...
- 为什么SSL证书流量暴增?
网络服务提供商 Sandvine 近日发布了一份报告,中提到了一个非常有趣的现象:和去年的数据相比,加密网络流量(SSL)在今年正在呈现出爆发式增长. 这个变化在欧洲表现得十分明显:和去年的 1.47 ...
- JavaScript代码优化(下载时间和执行速度优化)
JavaScript代码的速度被分成两部分:下载时间和执行速度. 下载时间 Web浏览器下载的是js源码,因此所有长变量名和注释都回包含在内.这个因素会增加下载时间.1160是一个TCP-IP包中的字 ...
- svn cleanup failed–previous operation has not finished 解决方法
今天svn遇到一个头疼的问题,最开始更新的时候失败了,因为有文件被锁住了.按照以往的操作,我对父目录进行clean up操作,但是clean up 操作也失败了! svn cleanup failed ...
- IOS中类和对象还有,nil/Nil/NULL的区别
转自:http://blog.sina.com.cn/s/blog_5fb39f910101akm1.html 类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性 ...