[ACM] POJ 2253 Frogger (Shortest path deformation, minimum of the longest edge in each path)

Source: Internet
Author: User
Tags cmath

Frogger
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24879 Accepted: 8076

Description

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who was sitting on another stone. He plans to visit she, but since the water was dirty and full of tourists ' sunscreen, he wants to avoid swimming and instea D reach her by jumping.
Unfortunately Fiona ' s stone is out of the his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach hers by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must is at least as long as the longest jump occuring In the sequence.
The frog distance (humans also call it minimax distance) between both stones therefore is defined as the minimum necessary Jump range possible paths between the stones.

You is 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 would contain one or more test cases. The first line of all test case would contain the number of stones N (2<=n<=200). The next n lines each contain the integers xi,yi (0 <= xi,yi <=) representing the coordinates of stone #i. ston E #1 is Freddy's stone, stone #2 is Fiona's Stone, the other n-2 stones was 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 was replaced by the TES The T Case number (they was numbered from 1) and Y was replaced by the appropriate real number, printed to three decimals. Put a blank line after all 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

Source

field=source&key=ulm+local+1997 "style=" Text-decoration:none ">ulm Local 1997


Problem Solving Ideas:

To test instructions not long in English to see several times, only clear what is called the frog distance.

There are n blocks of stone. 1-n. Each stone has x, y coordinates, the frog standing on the first stone, frog second standing on the second stone, the frog a number to seek through the N-Stone to find the frog second, because the frog can step on no matter what a stone, so from the first stone to the second stone there are very many paths, if x, In each path, there is a range of jumps (that is, the maximum distance between two stones in this path), so one has a common x jump range. What we're asking for is the minimum value of this x jump range.   is the frog distance. For example, there are two pathways 1 (4) 5 (3) 2 for 1 to 5 for the edge between 4 and 5 to 2 for 3. Then the path jumping range (the maximum distance between two stones) is 4, there is a path 1 (6) 4 (1) 2, the jump range of the path is 6, the two paths jump range is 4, 6, we ask for the smallest jump range, that is, 4.

Edge traversal and update of point values. This point value represents the frog distance from stone number 1th to the first [I] block of stone.

With the floyed algorithm and the Dijkstra algorithm, the update point value of the statement can be modified.

Code:

Floyed:

#include <iostream> #include <cmath> #include <iomanip> #include <string.h> #include < algorithm>using namespace Std;const int maxnode=210;double mp[maxnode][maxnode];int nodenum;struct P{int x, y;} Point[maxnode];d ouble dis (P a,p b) {return sqrt ((B.Y-A.Y) * (B.Y-A.Y) + (b.x-a.x) * (b.x-a.x));} void Floyed () {for (int k=1;k<=nodenum;k++) for (int. i=1;i<=nodenum;i++) for (int j=1;j<=nodenum;j+    +) Mp[i][j]=min (Mp[i][j],max (Mp[i][k],mp[k][j]));//The smallest side of the longest edge in many paths}int main () {int c=1; while (Cin>>nodenum&&nodenum) {for (int i=1;i<=nodenum;i++) cin>>point[i].x>        >point[i].y; for (int i=1;i<=nodenum;i++) for (int. j=i+1;j<=nodenum;j++) {Mp[i][j]=mp[j][i]=dis (point        [I],point[j]);        } floyed ();        cout<< "Scenario #" <<c++<<endl; Cout<<setiosflags (ios::fixed) <<setprecision (3) << "Frog Distance =" <<mp[1][2]<<endl;    cout<<endl; } return 0;}

Dijkstra:

#include <iostream> #include <string.h> #include <algorithm> #include <iomanip> #include < cmath>using namespace Std;const int maxn=210;const int inf=0x3f3f3f3f;double MP[MAXN][MAXN];d ouble dist[maxn];bool Vis[maxn];int n;struct p{int x, y;} POINT[MAXN];d ouble dis (P a,p b) {return sqrt ((B.Y-A.Y) * (B.Y-A.Y) + (b.x-a.x) * (b.x-a.x));}    void Dijkstra (int start) {memset (vis,0,sizeof (VIS));    memset (dist,inf,sizeof (Dist));    for (int i=1;i<=n;i++) Dist[i]=inf;    dist[start]=0;        for (int i=1;i<=n;i++) {int minnum,min=inf;            for (int j=1;j<=n;j++) if (!vis[j]&&dist[j]<min) {minnum=j;        MIN=DIST[J];        } vis[minnum]=1;     for (int j=1;j<=n;j++) dist[j]=min (Dist[j],max (Dist[minnum],mp[minnum][j])),//dis[j] is the smallest edge in the longest edge of the entire path from the stone to the J    }}int Main () {int c=1; while (Cin>>n&&n) {for (int i=1;i<=n;i++) cin>>point[i].x>>point[i].y; for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) {Mp[i][j]=mp[j][i]=dis (Point[i],            POINT[J]);        } Dijkstra (1);        cout<< "Scenario #" <<c++<<endl;        Cout<<setiosflags (ios::fixed) <<setprecision (3) << "Frog Distance =" <<dist[2]<<endl;    cout<<endl; } return 0;}

Note: Double arrays are not easy to copy with Memset. You also have to consider byte lengths.


[ACM] POJ 2253 Frogger (Shortest path deformation, minimum of the longest edge in each path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.