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

Frogger

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

Description

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 题目大意:给出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 (最长路中的最短路)