二分搜索+DFS

Frogger

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 59630   Accepted: 18675

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

   注意!!!最后输出结果的是不要写成%.3lf(会WR),要改成%.3f(我也不知道为什么,哈哈哈);

 

   刚学了最短路,那些什么dij,spfa,、、、想了很久,没想到解决的办法。然后就莫名奇妙的想到了二分搜索加DFS来解决这个问题;  

      首先1 到 2  的距离,最大就是1和2的直线距离,然后我们要通过其他点来缩小这个距离。(这里的距离指的是可以从1 到 2 的所有线段中的最大的一段),最后我们要求的就是最小的距离。那这个点我们开始就初始化这个最小的距离为0,然后利用二分去缩小这个范围到0.0001(因为题目保留了三位小数)。在缩小范围时我们可以用DFS去判断这个距离到底是不是不是可以从1 到 2的;

  • #include<vector>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #define ll long long
    using namespace std;
    #define INF 0x3f3f3f3f
    int n,vis[220];
    double mp[220][220],dis[220];
    struct AX{
        int x;
        int y;
    }point[220];
    
    vector<double> mx[220];
    double Get_dis(AX a,AX b){
        int x=a.x-b.x,y=a.y-b.y;
        return sqrt(x*x*1.0+y*y*1.0);
    }
    
    bool DFS(int k,double v){
        vis[k]=1;
        for(int i=1;i<=n;i++){
            if(vis[i])continue;
            if(mp[k][i]>v)continue;
            else if(i!=2){
                if(DFS(i,v)) return true;}
            else
                return true;
        }
        return false;
    }
    
    int main(){
        int casen=1;
        while(scanf("%d",&n)==1&&n){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(i==j)mp[i][j]=0.0;
                    else mp[i][j]=INF*1.0;
    
            for(int i=1;i<=n;i++)scanf("%d%d",&point[i].x,&point[i].y);
    
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    mp[i][j]=Get_dis(point[i],point[j]);
    
            double R=mp[1][2],L=0.0,mid;
            while(R-L>0.00001){
                mid=(L+R)*1.0/2;
                memset(vis,0,sizeof(vis));
                if(DFS(1,mid))R=mid;
                else
                    L=mid;
            }
    
            printf("Scenario #%d\n",casen++);
            printf("Frog Distance = %.3f\n",mid);
            printf("\n");
    
        }
        return 0;
    }
    

猜你喜欢

转载自blog.csdn.net/xiaonanxinyi/article/details/81950269
今日推荐