POJ[2253] Frogger 【最短路变形】

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的路径,使得该路径上的最大边权是所有1~2的路径中最大边权的最小值。

分析:

这题是最短路的变形,修改Dijkstra算法中dis数组的定义,在dis数组中保存从起始点到达当前点的所有路径中最大边权的最小值,并用当前点的dis和相邻边的最大值去更新相邻点的dis值。

具体解释见代码。

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
#include <iomanip>

using namespace std;

const int maxn=205;
const int INF=0x3f3f3f3f;

int vis[maxn];
double dis[maxn],tm[maxn];

int head[maxn];
int ans;
int e,n;

struct node{
 
    int u,v;
    
    double w;
 
    int next;
 
}edge[maxn*maxn];

void add(int u,int v,double w){//向邻接表中加边 
 
    edge[ans].u=u;
 
    edge[ans].v=v;
 
    edge[ans].w=w;
 
    edge[ans].next=head[u];
 
    head[u]=ans++;
 
}

struct dot{	//存储点的坐标 
	int x,y;
	dot(){}
	dot(int x,int y):x(x),y(y) {}
}; 

dot po[205];

struct point{	//定义优先队列中的节点 
    int id;
    double val;
    point(int id,double val):id(id),val(val) {}
    bool operator <(const point &x)const{
        return val>x.val;
    }
};

void dijkstra(int s){
    memset(vis,0,sizeof(vis));
    for(int i=1; i<=n; i++){
    	dis[i]=INF;
	}
    priority_queue<point> q;
    q.push(point(s,0));
    vis[s]=1;
    dis[s]=0;
    while(!q.empty()){
        int cur=q.top().id;
        q.pop();
        vis[cur]=1;	//优先队列中最小的值不可能再被经过其他节点的路径更新 
        if(cur==2) return;	//dis[2]算完即可退出; 
        for(int i=head[cur]; i!=-1; i=edge[i].next){
            int id=edge[i].v;
            if(!vis[id] && max(dis[cur],edge[i].w) < dis[id]){	//用当前点的dis和相邻边的最大值去更新相邻点的dis值
                dis[id]=max(dis[cur],edge[i].w);
                q.push(point(id,dis[id]));
            }
        }
    }
}

int main(){
	int cas=0;
	while(scanf("%d",&n)!=EOF&&n){
		cas++;
		ans=0;
		memset(head,-1,sizeof(head));
		int tx,ty;
		for(int i=1;i<=n;i++){
			scanf("%d%d",&tx,&ty);
			po[i]=dot(tx,ty);
		}
		//建图 
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				double dist=sqrt((double)(po[i].x-po[j].x)*(po[i].x-po[j].x)+(po[i].y-po[j].y)*(po[i].y-po[j].y));
				add(i,j,dist);
				add(j,i,dist);
			}
		}
		dijkstra(1);
		printf("Scenario #%d\nFrog Distance = %.3f\n\n", cas, dis[2]);	//注意要多输出一个空行 
	}
	return 0;
}
发布了30 篇原创文章 · 获赞 5 · 访问量 900

猜你喜欢

转载自blog.csdn.net/qq_42840665/article/details/101206500