POJ2253(dijkstra堆优化)

https://vjudge.net/problem/POJ-2253

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists‘ sunscreen, he wants to avoid swimming and instead reach her by jumping.
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

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy‘s stone, stone #2 is Fiona‘s stone, the other n-2 stones are unoccupied. There‘s a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

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
 1 //#include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 #include<queue>
 9 #define maxn 210
10 #define ms(x,n) memset(x,n,sizeof x);
11 const int inf=0x3f3f3f3f;
12 using namespace std;
13 int n;
14 double d[maxn],cost[maxn][maxn];
15 bool vis[maxn];
16 struct node
17 {
18     int x,y;
19     node(int xx,int yy){x=xx,y=yy;}
20 };
21 vector<node> v;
22 double dis(double x1,double y1,double x2,double y2)
23 {
24     return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
25 }
26 typedef pair<double,int> p;
27
28 void dij(int s)
29 {
30     fill_n(d,maxn,inf);
31     ms(vis,0);
32     priority_queue<p,vector<p>,greater<p> >q;
33     q.push(p(d[s]=0,s));
34     while(!q.empty())
35     {
36         p cur=q.top();
37         q.pop();
38         int i=cur.second;
39         if(vis[i])continue;
40         vis[i]=1;
41         for(int j=0;j<n;j++)
42         {
43             if(max(d[i],cost[i][j])<d[j])
44             {d[j]=max(d[i],cost[i][j]);
45              q.push(p(d[j],j));
46             }
47         }
48     }
49 }
50 int main()
51 {
52     int t=0;
53     while(~scanf("%d",&n),n)
54     {
55         int x,y;
56         v.clear();
57         for(int i=1;i<=n;i++)
58         {
59             scanf("%d%d",&x,&y);
60             v.push_back(node(x,y));
61         }
62         fill_n(cost[0],maxn*maxn,inf);
63         for(int i=0;i<n;i++)
64             for(int j=i+1;j<n;j++)
65             cost[i][j]=cost[j][i]=dis(v[i].x,v[i].y,v[j].x,v[j].y);
66         dij(0);
67         if(t)cout<<endl;
68        // printf("Scenario #%d\nFrog Distance = %.3f\n",t++,d[1]);
69        printf("Scenario #%d\nFrog Distance = %.3f\n", ++t, d[1]);
70
71     }
72     return 0;
73 }

原文地址:https://www.cnblogs.com/zuiaimiusi/p/10777147.html

时间: 2024-10-20 19:32:34

POJ2253(dijkstra堆优化)的相关文章

hdu 2544 单源最短路问题 dijkstra+堆优化模板

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 41168    Accepted Submission(s): 17992 Problem Description 在每年的校赛里.全部进入决赛的同学都会获得一件非常美丽的t-shirt.可是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的.所以如今他们想要寻

Bzoj 2834: 回家的路 dijkstra,堆优化,分层图,最短路

2834: 回家的路 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 62  Solved: 38[Submit][Status][Discuss] Description Input Output Sample Input 2 1 1 2 1 1 2 2 Sample Output 5 HINT N<=20000,M<=100000 Source dijkstra+堆优化+分层图 把所有的横向和纵向分开看.跑最短路即可. 注意:N这么大,不能写

POJ 3013 Big Christmas Tree【最短路变形,DIjkstra堆优化+spfa算法】

Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23064   Accepted: 4987 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of t

【日常学习】【Dijkstra堆优化】codevs2038 香甜的黄油题解

转载请注明出处 [ametake版权所有]http://blog.csdn.net/ametake 先放上题目,出自USACO 题目描述 Description 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费用在奶牛上. 农夫John很狡猾.他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场.他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶. 农

Heap+Dijkstra堆优化的Dijkstra

前面说到"原生的Dijkstra",由于Dijkstra采用的是贪心策略,在贪心寻找当前距离源结点最短的结点时需要遍历所有的结点,这必然会导致效率的下降,时间复杂度为n^n.因此当数据量较大时会消耗较长时间.为了提高Dijkstra的效率,只有对Dijkstra的贪心策略进行改进. 由于Dijkstra采用的贪心策略是每次寻找最短距离的结点并将其放入存放所有已知最短距离结点的S集合中,可以联想到堆以及优先级队列这些数据结构,这些结构都能非常高效地提供当前状态距离最短的结点.实践也可以证

Poj 1151 Invitation Cards dijkstra 堆优化

很裸的最短路,不过节点数和边数都是1e6,直接dij肯定是不行了,稀疏图用heap优化一下就好 o(╯□╰)o注意STL里面的优先队列是优先级大的(值大的)在前面的,一开始没注意WA了好几发,哎,太粗心了 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #inc

[转]浅谈dijkstra堆优化

众所周知的,dijkstra是图论算法中求单源最短路的一种简单求法.可能有人会说SPFA比dijkstra要实用,而且可以用于求存在负边权的情况,但是dijkstra有着他的优点——其运行速度上优于SPFA. (PS.需要堆进行优化.) 我们先看一道经典(水)题: 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线. 若有连线,则表示可从一个点到达另一个点,即两点之间有通路,通路的距离为两点之间的直线距离.现在的任务是找出从入点到出点之间的最短路

lfyzoj1096 最短路径问题 dijkstra堆优化

题目描述 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线. 若有连线,则表示可从一个点到达另一个点,即两点间有通路,通路的距离为两点间的直线距离.现在的任务是找出从一点到另一点之间的最短路径. 输入 第一行为整数n. 第2行到第n+1行(共n行) ,每行两个整数x和y,描述了一个点的坐标. 第n+2行为一个整数m,表示图中连线的个数. 此后的m 行,每行描述一条连线,由两个整数i和j组成,表示第i个点和第j个点之间有连线. 最后一行:两个整数

[Usaco2010 Feb]Chocolate Giving 最短路dijkstra+堆优化

本人水平有限,题解不到为处,请多多谅解 本蒟蒻谢谢大家观看 题目:传送门 最短路板子题:迪杰斯特拉+堆优化 注意:因为我建的是大根堆,所以要将距离取负,再存入大根堆堆中,这样队首就是最小值 直接套模板即可 code: 1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 #pragma GCC optimize(3) 4 const int N=50005; 5 const int M=200002; 6 using namespace st