题意:

题目撰写者的英语真是艰难晦涩,看了别人题解,才知道这题题意。

两个forger 一个froger 要蹦到另外一个froger处,他们的最短距离是这样定义的 :

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.

即为 :两个石头之间的frog distance就是在这两个石头之间的所有路径中最大跳(necessary jump range)最小的。 (以上引自aowarmen's blog)

题目要我们求解的frog distance和dijksta算法中的最短路径距离不是同一个概念。

分析:

理解了题意之后,我们把Dijsktra中的松弛条件改成:

dist[i] = max( dist[u],edge[u][i])  //u为某一中间节点,dist[i]表示源点到结点i的距离  (以上摘自Eucalyptus

同时,我们可以优化距离的开根号运算,两点之间的距离定义改成:欧几里德距离的平方和。

这样一来,我们只需要对结果开根号即可,节省中间的运算时间。

提交时请不要用g++,要用c++编译器,同样的代码,我用G++提交时总是WA

代码:

优先队列实现的Dijkstra模板,只要修改对应的松弛条件即可。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 201
#define inf 0x3f3f3f3f
#define eps 0.0001
typedef pair<double,int> P;
double max(double a,double b){
return (a>b?a:b);
}
struct points {
int x, y;
points(int xi,int yi){ x=xi,y=yi;}
points(){x=0,y=0;}
}; points nodes[maxn];
double map[maxn][maxn];
double dist[maxn];
double cal_dist(points a,points b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
void init(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
map[i][j]=cal_dist(nodes[i],nodes[j]);
map[j][i]=map[i][j];
}
}
} void dijkstra(int s,int n){
priority_queue<P,vector<P>,greater<P> > Q;
bool visited[maxn];
memset(visited,0,sizeof(visited));
for(int i=1;i<=n;i++)
dist[i]=inf;
dist[s]=0.0;
Q.push(P(0.0,s)); while(!Q.empty()){
int v=Q.top().second;
Q.pop();
if(visited[v]) continue;
visited[v]=true;
for(int i=1;i<=n;i++){
double local_max=max(dist[v],map[v][i]);
if(dist[i]-local_max>eps){
dist[i]=local_max;
Q.push(P(local_max,i));
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
int cases=0,n;
while(scanf("%d",&n)!=EOF && n){
cases++;
for(int i=1;i<=n;i++)
scanf("%d %d",&nodes[i].x,&nodes[i].y);
init(n);
dijkstra(1,n);
printf("Scenario #%d\n",cases);
printf("Frog Distance = %.3lf\n\n",sqrt(dist[2]));
}
}

普通方法实现的Dijkstra算法

#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 210
#define inf 0x3f3f3f3f
typedef pair<double,int> P;
double max(double a,double b){
return (a>b?a:b);
}
struct point {
double x, y;
point(double xi,double yi){ x=xi,y=yi;}
point(){x=0,y=0;}
}; point nodes[maxn];
double map[maxn][maxn];
double dist[maxn];
double cal_dist(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) map[i][j]=0.0;
else
map[i][j]=cal_dist(nodes[i],nodes[j]);
map[j][i]=map[i][j];
}
}
} void dijkstra(int s,int n){
bool visited[maxn];
memset(visited,0,sizeof(visited));
visited[s]=true;
for(int i=1;i<=n;i++)
dist[i]=map[s][i];
dist[s]=0.0;
for(int i=1;i<=n;i++){
double min=inf;
int u=0;
for(int j=1;j<=n;j++)
if(!visited[j] && dist[j]<min){
min=dist[j];
u=j;
}
visited[u]=true;
if(min==inf) break;
for(int j=1;j<=n;j++){
double tmp=max(dist[u],map[u][j]);
if(!visited[j]&&dist[j]>tmp)
dist[j]=tmp;
}
}
} int main(){
//freopen("in.txt","r",stdin);
int cases=0,n;
while(scanf("%d",&n)!=EOF && n){
cases++;
for(int i=1;i<=n;i++)
scanf("%lf %lf",&nodes[i].x,&nodes[i].y);
init(n);
dijkstra(1,n);
printf("Scenario #%d\n",cases);
printf("Frog Distance = %.3lf\n\n",dist[2]);
}
}

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 - [dijkstra求最短路]

    Time Limit: 1000MS Memory Limit: 65536K Description Freddy Frog is sitting on a stone in the middle ...

  3. POJ. 2253 Frogger (Dijkstra )

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

  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. 最短路(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【最短路变形——路径上最小的最大权】

    链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  8. poj 2253 Frogger (dijkstra最短路)

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

  9. POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】

    Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  10. poj 2253 Frogger 最小瓶颈路(变形的最小生成树 prim算法解决(需要很好的理解prim))

    传送门: http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

随机推荐

  1. (转) RSA算法原理(一)

    最近用到了RSA加密算法,虽然有现成的,但是想看看它的原理,翻到此文,感觉写得很好,通俗易懂,转了.   作者: 阮一峰 日期: 2013年6月27日 如果你问我,哪一种算法最重要? 我可能会回答&q ...

  2. sdk墙内更新方法

    因为GFW有“保护”,我们能“安全”的遨游在中华互联局域网内.如何快速地更新sdk,一直是Android开发者的心病.网上流传着五花八门的方法,在这我记录一些我用过的切实可行的方法供给有需要的人.同时 ...

  3. [CF#250 Div.2 D]The Child and Zoo(并查集)

    题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...

  4. MATLAB axis和axes的区别

    axis中文为“轴”之意,在matlab中用于控制坐标轴的范围和样式(颜色等). axis([XMIN XMAX YMIN YMAX]) 设置当前所绘图像的x轴和y轴的范围.axis([XMIN XM ...

  5. JavaScript学习笔记-商品管理新增/删除/修改功能

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  6. C 语言学习的第 04 课:编译器常见错误和警告(1)

    同学们可能已经开始使用 C-Free 5 写自己的程序了.但是新手编程,总是会有一些磕磕绊绊.不要紧,在这篇文章中,就主要来了解一些编程开始时经常会遇到的语法方面的问题. warning: no ne ...

  7. 删除root子目录,如何恢复子目录配置文件

    手贱,一不小心rm -rf 问题描述:删除/root/子目录文件(含隐藏配置文件)shell变成-bash-4.2#,如何恢复原貌 解决方法: root用户进入,自己配置相关文件:mkdir /roo ...

  8. 【jQuery EasyUI系列】创建CRUD数据网格

    在上一篇中我们使用对话框组件创建了CRUD应用创建和编辑用户信息.本篇我们来创建一个CRUD数据网格DataGrid 步骤1,在HTML标签中定义数据网格(DataGrid) <table id ...

  9. Criteria查询之sqlRestriction()的理解

    sqlRestriction()的理解 在Criteria查询中 使用sqlRestriction()方法来提供SQL语法作限定查询,作为where字句 查看官方给的例子,如下 List cats = ...

  10. Java发送邮件代码

    MailSenderInfo.java package com.nihaorz.mail.util; import java.util.Properties; public class MailSen ...