题意:

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

两个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. iframe在ios下无故扩大的问题探究

    移动端页面内嵌了个 iframe,在 ios 下打开却发现页面怪异.比如 demo.代码如下: <!DOCTYPE html> <html lang="zh-CN" ...

  2. .net破解二(修改dll)

    多谢大家支持! 昨天说了一下反编译与剥壳(.net破解一(反编译,反混淆-剥壳,工具推荐)),今天就来修改修改dll,为了方便,我自己写一个简单程序用来测试 代码如下: 一个 ConsoleAppli ...

  3. Scala函数式编程进阶

    package com.dtspark.scala.basics /** * 函数式编程进阶: * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: * 2, 函数更长用的方式 ...

  4. Validform表单验证总结

    近期项目里用到了表单的验证,选择了Validform_v5.3.2. 先来了解一下一些基本的参数: 通用表单验证方法:Demo: $(".demoform").Validform( ...

  5. mysql中导入txt文件

    1 windows 下 mysql导入txt文件(使用mysql的workbench) load data local infile 'path' into table table_name fiel ...

  6. 40-cut 简明笔记

    从输入行中选取字符或者字段 cut [options] [file-list] cut 从输入行中选取字符或者字段,并将他们写到标准输出,字符和字段从1开始编号 参数 file-list 是文件的路径 ...

  7. 转 -- linux IO子系统和文件系统读写流程

    我们含有分析的,是基于2.6.32及其后的内核. 我们在linux上总是要保存数据,数据要么保存在文件系统里(如ext3),要么就保存在裸设备里.我们在使用这些数据的时候都是通过文件这个抽象来访问的, ...

  8. 云平台 为什么推荐使用小VM 而不是大VM独占宿主机的方式部署游戏服?

    近期公司X游戏项目,提了一个游戏VM资源的需求,是 64GB RAM + 30Core CPU 的VM规格,而一个VM部署10个游戏服.而我们云平台推荐的VM规格为 4 Core CPU + 4GB ...

  9. Java对象的访问

    对象访问在Java语言中无处不在,即使最简单的访问也涉及Java栈.Java堆.方法区这三个重要的内存区域中. 例:Object obj = new Object(); Object obj     ...

  10. uva10870 矩阵

    f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d, 可以用矩阵进行优化,直接构造矩阵,然后快 ...