首页 > 代码库 > POJ 2253 Frogger
POJ 2253 Frogger
Frogger
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26519 | Accepted: 8633 |
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.
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
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414
二分最大距离 + 记忆化dfs一条可行路...
#include <iostream>#include <vector>#include <queue>#include <cmath>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int N = 210;const int inf = 1e9+7 ;const double eps = 1e-7;int n , m ;double dis[N][N] , x[N] , y[N];inline double DIS( int a , int b ){ return sqrt((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b]));}bool vis[N] ;struct node{ int v ; double w; node(){}; node( int a , double b ) { v =a , w = b; }};vector<node>g[N];bool tag ;void dfs( int u ,double DIS ){ if( vis[u] || tag ) return ; if( u == 2 ) { tag = true ; return ; } vis[u] = true ; for( int i = 0 ; i < g[u].size(); ++i ){ int v = g[u][i].v ; double w = g[u][i].w ; if( DIS + eps < w ) continue ; dfs( v , DIS ); }}bool test( double DIS ){ tag = false; memset( vis , false , sizeof vis ); dfs( 1, DIS ); if( tag ) return true; return false;}void run(){ for( int i = 0 ; i <= n ; ++i ) g[i].clear() ; for( int i = 1 ; i <= n ; ++i ) cin >> x[i] >> y[i] ; for( int i = 1 ; i <= n ; ++i ){ for( int j = i + 1 ; j <= n ; ++j ){ double w = DIS( i , j ); g[i].push_back(node(j,w)); g[j].push_back(node(i,w)); } } double ans = 0 , l = 0.0 , r = DIS(1,2) + 10.0; while( l + eps <= r ){ double mid = (l+r) / 2.0 ; if( !test(mid) ) l = mid + eps ; else ans = mid , r = mid - eps ; } printf("%.3lf\n\n",ans);}int main(){ #ifdef LOCAL freopen( "in.txt","r",stdin ); #endif // LOCAL ios::sync_with_stdio(false); int cas = 1 ; while( cin >> n && n ) cout << "Scenario #"<< cas++ <<endl << "Frog Distance = " , run();}
POJ 2253 Frogger
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。