POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 题目大意:给出n个点的坐标,源点是1,终点是2。问你一条从源点能到达终点的路径中的最长边且满足不大于其他可到达终点路径中的最小边长度。(即求最长边的最小值)。 解题思路:改变Dijkstra中的d数组的含义,在更新d数组的时候条件也变一下。d[i]表示从源点到i点的最长边最小值。那么更新条件就变为if( d[i] > max( d[u] ,distance(u,i) ) ) d[i] = max(d[u] , distance (u,i) )。表示当前在u点时,如果路径中的最长边长度,跟 distance( u ,i )的最大值还小于d[i](i点的最大边长度),那么说明原来到达i点时的最长边还不够小,那么更新即可。
结果必须输出%.3f,而不是%.3lf。这里一直错。
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<string.h>
using namespace std;
const int maxn = 1e4;
const int INF = 0x3f3f3f3f;
struct HeapNode{
double d;
int u;
bool operator < (const HeapNode &rhs)const {
return d > rhs.d;
}
};
struct Edge{
int from,to;
double dist;
};
struct node{
double x,y;
}cor[maxn];
priority_queue<HeapNode>PQ;
vector<Edge>edge;
vector<int>G[maxn];
double d[maxn];
int vis[maxn];
int n,m;
double distan(node a, node b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void AddEdge(int u,int v,double dis){
edge.push_back((Edge){u,v,dis});
m = edge.size();
G[u].push_back(m-1);
}
void Dijstra(int s){
for(int i = 0; i<= n;i++){
d[i] = INF;
}
memset(vis,0,sizeof(vis));
d[s] = 0.00;
PQ.push( (HeapNode){d[s],s} );
while(!PQ.empty()){
HeapNode x = PQ.top();
PQ.pop();
int u = x.u;
if(u == 1){
break;
}
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0; i < G[u].size(); i++){
Edge & e = edge[G[u][i]];
if( (!vis[e.to]) && d[e.to] > max( d[e.from], e.dist) ){
d[e.to] = max(d[e.from] , e.dist);
PQ.push( (HeapNode){ d[e.to] , e.to } );
}
}
}
}
void init(int n){
for(int i = 0; i <= n;i++){
G[i].clear();
}
edge.clear();
while(!PQ.empty())
PQ.pop();
}
int main(){
int cnt = 0;
while(scanf("%d",&n)!=EOF && n){
init(n);
for(int i = 0; i < n; i++){
scanf("%lf%lf",&cor[i].x,&cor[i].y);
for(int j = 0; j < i; j++){
AddEdge( i, j, distan(cor[i],cor[j]));
AddEdge( j, i, distan(cor[j],cor[i]));
}
}
Dijstra(0);
printf("Scenario #%d\n",++cnt);
printf("Frog Distance = %.3f\n",d[1]);
puts("");
}
return 0;
} /*
这里给出4个点6条边,不是坐标表示
4 6
1 4 3
1 2 4
1 3 5
2 4 2
3 4 6
2 3 8
可以看出为什么需要改成那样的更新条件 */
POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】的更多相关文章
- POJ 2253 Frogger -- 最短路变形
这题的坑点在POJ输出double不能用%.lf而要用%.f...真是神坑. 题意:给出一个无向图,求节点1到2之间的最大边的边权的最小值. 算法:Dijkstra 题目每次选择权值最小的边进行延伸访 ...
- POJ 2253 Frogger 最短路 难度:0
http://poj.org/problem?id=2253 #include <iostream> #include <queue> #include <cmath&g ...
- POJ 2253 Frogger (最短路)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28333 Accepted: 9208 Descript ...
- poj 2253 Frogger(最短路 floyd)
题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...
- 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可行 ...
- 最短路(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 ...
随机推荐
- ML 徒手系列 SVM
在Lagrange乘子法中,介绍了如何构建及如何使用对偶函数,对目标问题进行求解. 这一章利用L乘子法对SVM进行推导. SVM 即支持向量机,是通过求解超平面进行分类的一种算法.所谓的支持向量,是在 ...
- MongoDB一些基本的命令
Win+R进入Dos命令窗口,输入cmd,进入MongoDB exe文件的所在目录,比如我的在E:\MongoDB\bin,分别执行:“E:”回车,然后:"cd mongodb/bin&qu ...
- Learning Python 010 函数 1
Python 函数 1 调用函数 举个例子 多于Python内部的函数,你可以在Python的交互式终端中使用help()函数来查看函数的使用方法.比如:abs()函数,如果你不知道如何使用它,你可以 ...
- 6.5 通过命令安装git
本来是一头雾水,以为很麻烦,结果,一句命令就搞定: 安装完后,在搜索框搜索,即出现github.
- C++ 从内存的角度,学习虚继承机制
测试代码 #include <stdio.h> struct AA { char b; char b1; int b3; char b2; }; class A { public: A() ...
- jquery插件-自由拖拽
最近工作不是很忙,学习之余想整理一些代码出来,首先想到的就是是js拖拽. 两年前去某公司面试的时候,曾经被问过这个问题,如何在页面上拖放元素,尽管现在看起来很简单,但当时的我半点思路都没有,面试想当然 ...
- js复习笔记
isNaN(x) 1.判断结果不是纯数字, var a=1234var b =isNan(a) //b是 false 因为a是纯数字 var a="abc123"var b =is ...
- 聊聊 CDN 缓存与浏览器缓存
CDN 是互联网上内容分发的重要一环.无论您之前是否了解过 CDN,其实它已经在您的日常生活中发挥作用了.比如您正在淘宝挑选心仪的商品,或者在观看一段令人捧腹的视频,以及您正在阅读的这篇文章,这些资源 ...
- CAS客户端整合(四)-- Cacti
Cacti 是一套纯 lnmp 搭建的服务器监控系统,用 SNMP 抓取数据,RRDTool 绘制表格 登录流程 Cacti 的登录同样是先判断session,再尝试从 cookie 读取 sessi ...
- UML——初识
初识 刚刚接触到UML的时候,先看的书,对整本书的内容做了宏观的把控.感觉UML这个东西和自己想象中的不一样.起初我认为它只是一个工具,将软件开发过程中不同的阶段用不用种类的图表现出来,后来才发现它是 ...