您的位置:首页 > 其它

[POJ 2253][最短路dijkstra]Frogger

2017-08-19 19:18 281 查看

Frogger

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 48185 Accepted: 15329

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

Source

Ulm Local 1997

题意

给定n块石头的坐标,求第一块石头到第二块石头的Frog Distance。

Frog Distance 为从起点到终点路径中所有边的最大值中的最小值。

题解

由于从每块石头都能跳到任意一块石头,于是我们就没必要建边了。直接跑Dijkstra,把Dijkstra中相邻的边中更新dis变小的点变为更新所有石头中dis变小的点即可。由于求的是最短路中最长边的最小值,dis更新的条件变为(i != x && (dis[i] > dd && dis[i] > dis[x]))即可,即下一块石头与当前石头不是同一块, dis大于两块石头件的距离并且比前一节点的dis大。跑出来的dis[2]即为所求答案。

//#define LOCAL
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

#define LL long long
#define ll long long
#define ull unsigned long long
#define GPQ priority_queue<int, vector<int> greater<int> >
#define inf INF
#define INF 0x3f3f3f3f
#define maxn MAX_N
#define MOD mod
#define MMT(x,i) memset(x,i,sizeof(x))
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second

const LL MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-8;

const int MAX_N = 210;
int n;
double x[MAX_N], y[MAX_N];
double dis[MAX_N];
double getdis(int i, int j)
{
return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}
void dijkstra(int s)
{
priority_queue<int, vector<int>, greater<int> > que;
FOR(i, n) dis[i] = INF;
dis[s] = 0;
que.push(s);
while(!que.empty())
{
int x = que.top(); que.pop();
FOR(i, n)
{
double dd = getdis(i, x);
if(i != x && (dis[i] > dd && dis[i] > dis[x]))
{
dis[i] = max(dd, dis[x]);
que.push(i);
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int cas = 1;
while(~scanf("%d", &n))
{
if(n == 0)  break;
FOR(i, n)
scanf("%lf%lf", &x[i], &y[i]);
dijkstra(1);
printf("Scenario #%d\n", cas++);
printf("Frog Distance = %.3f\n\n", dis[2]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: