首页 > 代码库 > POJ 2253 Frogger

POJ 2253 Frogger

Frogger

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2253
64-bit integer IO format: %lld      Java class name: Main
 
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

Source

Ulm Local 1997
 
解题:最小生成树算法 以及 dijkstra Floyd都可以啊。
 
先上Prim算法版
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 210;18 double dis[maxn][maxn],d[maxn];19 bool vis[maxn];20 vector<int>g[maxn];21 int n,x[maxn],y[maxn],p[maxn];22 void prim() {23     for(int i = 0; i <= n; ++i) {24         vis[i] = false;25         d[i] = INF;26         p[i] = -1;27     }28     d[1] = 0;29     while(true) {30         double minV = INF;31         int index = -1;32         for(int i = 1; i <= n; ++i)33             if(!vis[i] && d[i] < minV) minV = d[index = i];34         if(index == -1 || minV >= INF) break;35         if(p[index] > -1) {36             g[p[index]].push_back(index);37             g[index].push_back(p[index]);38         }39         vis[index] = true;40         for(int i = 1; i <= n; ++i)41             if(!vis[i] && d[i] > dis[index][i]) {42                 d[i] = dis[index][i];43                 p[i] = index;44             }45     }46 }47 double getDis(int i,int j) {48     double tmp = (x[i] - x[j])*(x[i] - x[j]);49     tmp += (y[i] - y[j])*(y[i] - y[j]);50     return sqrt(tmp);51 }52 double ans;53 bool dfs(int u,int fa,double maxV) {54     if(u == 2) {55         ans = maxV;56         return true;57     }58     for(int i = g[u].size()-1; i >= 0; --i) {59         if(g[u][i] == fa) continue;60         if(dfs(g[u][i],u,max(maxV,dis[u][g[u][i]]))) return true;61     }62     return false;63 }64 int main() {65     int cs = 1;66     while(scanf("%d",&n),n) {67         for(int i = 0; i <= n; ++i) g[i].clear();68         for(int i = 1; i <= n; ++i)69             scanf("%d %d",x+i,y+i);70         for(int i = 1; i <= n; ++i)71             for(int j = 1; j <= n; ++j)72                 dis[i][j] = getDis(i,j);73         prim();74         ans = 0;75         dfs(1,-1,0);76         printf("Scenario #%d\nFrog Distance = %.3f\n\n",cs++,ans);77     }78     return 0;79 }
View Code

 

Dijkstra版

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 210;18 double dis[maxn][maxn],d[maxn];19 int n,x[maxn],y[maxn];20 bool vis[maxn];21 double getDis(int i,int j) {22     double tmp = (x[i] - x[j])*(x[i] - x[j]);23     tmp += (y[i] - y[j])*(y[i] - y[j]);24     return sqrt(tmp);25 }26 void dijkstra(){27     for(int i = 1; i <= n; ++i){28         vis[i] = false;29         d[i] = INF;30     }31     d[1] = 0;32     while(true){33         double minV = INF;34         int index = -1;35         for(int i = 1; i <= n; ++i)36             if(!vis[i] && d[i] < minV) minV = d[index = i];37         if(index == -1 || minV >= INF) break;38         vis[index] = true;39         for(int i = 1; i <= n; ++i)40             if(!vis[i] && d[i] > max(d[index],dis[index][i]))41                 d[i] = max(d[index],dis[index][i]);42     }43 }44 int main() {45     int cs = 1;46     while(scanf("%d",&n),n) {47         for(int i = 1; i <= n; ++i)48             scanf("%d %d",x+i,y+i);49         for(int i = 1; i <= n; ++i)50             for(int j = 1; j <= n; ++j)51                 dis[i][j] = getDis(i,j);52         dijkstra();53         printf("Scenario #%d\nFrog Distance = %.3f\n\n",cs++,d[2]);54     }55     return 0;56 }
View Code

 

Floyd版

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 210;18 double dis[maxn][maxn];19 int n,x[maxn],y[maxn];20 double getDis(int i,int j) {21     if(i == j) return 0;22     double tmp = (x[i] - x[j])*(x[i] - x[j]);23     tmp += (y[i] - y[j])*(y[i] - y[j]);24     return sqrt(tmp);25 }26 void Floyd(){27     for(int k = 1; k <= n; ++k){28         for(int i = 1; i <= n; ++i){29             for(int j = 1; j <= n; ++j)30                 dis[i][j] = min(dis[i][j],max(dis[i][k],dis[k][j]));31         }32     }33 }34 int main() {35     int cs = 1;36     while(scanf("%d",&n),n) {37         for(int i = 1; i <= n; ++i)38             scanf("%d %d",x+i,y+i);39         for(int i = 1; i <= n; ++i)40             for(int j = 1; j <= n; ++j)41                 dis[i][j] = getDis(i,j);42         Floyd();43         printf("Scenario #%d\nFrog Distance = %.3f\n\n",cs++,dis[1][2]);44     }45     return 0;46 }
View Code

 

POJ 2253 Frogger