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 ...
随机推荐
- Unity3d:编辑器中运行正常,发布后的exe提示找不到文件
解决方案1:查看文件路径拼写方式,如果是用“+”拼接的,请改用System.IO.Path.Combine()方式拼接.经过测试,两种拼接方式打印出来的路径是一样的,但为什么 加号 的方式拼接unit ...
- SqlServer数据维护
现有两个表:Code和CodeCategory Code表: CodeCategory表: 现要把Code表中的数据如实维护一份数据,但是要设PlantID字段值为2,而ID要按规则自增并且要与Pla ...
- Linux下的TUN/TAP编程
linux下实现虚拟网卡我们在使用VMWARE的虚拟化软件时经常会发现它们能都能虚拟出一个网卡,貌似很神奇的技术,其实在Linux下很简单,有两种虚拟设 备,TUN时点对点的设备,tap表示以太网设备 ...
- HTML第七天学习笔记
今天主要是学习如何使用JS,第一个就是先是使用JS输出"Hello world" <!doctype html> <html lang="en" ...
- HTML第二天学习笔记
今天看视频学习的第一个知识是HTML中的块元素<div>和行内元素<span>. <!doctype html> <html lang="en&qu ...
- Android ndk下用AssetManager读取assets的资源
转自:http://www.cppblog.com/johndragon/archive/2012/12/28/196754.html 在使用 cocos2dx 在 Android 上进行游戏开发时, ...
- Vehicle Network Protocols -- ISO/KWP CAN CCD PCI SCI / SCP / Class 2
Vehicle Network Protocols There are 5 protocols in the OBD2 system and a car will normally only use ...
- UESTC 890 Card Trick(DP 纸牌魔术)
题意 给你一些牌 所有正面朝下放桌子上 你选一个起点 翻开那张牌 牌上的数字是几就向前走几步 J,Q,K 都是向前走10步 A向前走11步 知道向前走相应的步数后超过了终点 ...
- 使用TypeDescriptor给类动态添加Attribute
给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...
- HtmlWeb类
HtmlWeb类是一个从网络上获取一个HTML文档的类,其提供的功能大多是基于完成此需求出发.现在来来HtmlWeb类有哪些方法以及属性. 一.属性 bool AutoDetectEncoding { ...