Frogger

来源:互联网 发布:网络电视没有嘉佳卡通 编辑:程序博客网 时间:2024/05/29 18:04

Frogger

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 176   Accepted Submission(s) : 36
Problem 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
20 03 4317 419 418 50
 

Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414
题意:两只青蛙,a跳到b石头上去,它可以直接跳过去,他也可以通过跳到替他石头再跳过去,求一个数 代表的是一条从a到b的一条路径中的一个最大距离 输出三位有效数字
思路:

Shortest path faster algorithm

SPFA 其实就是Bellman-Ford的一种队列实现,减少了冗余,即松驰的边至少不会以一个d为∞的点为起点。

算法:

1.队列Q={s}

2.取出队头u,枚举所有的u的临边 .若d(v)>d(u)+w(u,v)则改进 ,pre(v)=u,由于d(v)减少了,v可能在以后改进其他的点,所以若v不在Q中,则将v入队。

3.一直迭代2,直到队列Q为空(正常结束),或有的点的入队次数>=n(含有负圈)。

#include<iostream>#include<stdio.h>#include<queue>#include<cmath>using namespace std;double x[1001],y[1001],d[1001];struct node{    int x;    int y;    }a[1001];double max(double a,double b){    return a>b?a:b;    }int main(){    int i,j,k;    int n;    int c=0;    while(cin>>n)    {        if(n==0)break;        for(i=1;i<=n;i++)        {            cin>>x[i]>>y[i];        }        for(i=1;i<=n;i++)        {            d[i]=1e10;        }        queue<int>q;        while(!q.empty())        {            q.pop();        }        q.push(1);        d[1]=0;        while(!q.empty())        {            k=q.front();            q.pop();            for(i=1;i<=n;i++)            {                if(i==k)                {                    continue;                }                double dis=max(sqrt((x[i]-x[k])*(x[i]-x[k])+(y[i]-y[k])*(y[i]-y[k])),d[k]);                if(dis<d[i])                {                    d[i]=dis;                    if(i!=2)                    {                        q.push(i);                    }                }            }        }        c++;        printf("Scenario #%d\n",c);        printf("Frog Distance = %.3f\n",d[2]);        printf("\n");    }    return 0;}
原创粉丝点击