码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2253(dijkstra堆优化)

时间:2019-04-27 00:37:02      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:sqrt   string   ops   second   ref   problem   term   cal   nim   

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 }

 

POJ2253(dijkstra堆优化)

标签:sqrt   string   ops   second   ref   problem   term   cal   nim   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!