码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2253 kruskal变形

时间:2014-09-11 17:04:32      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   strong   for   

 

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26226   Accepted: 8538

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


题目意思:
一个湖面上有n块石头,其中两个青蛙在两块石头上,青蛙1通过一个石头顺序跳到青蛙2处,求青蛙1与青蛙2之间路径中其中一条路径所有两块石头距离长度最大距离小于其他路径石头最小距离。

思路:
因为答案所属的路径中所有距离都小于其他路径中的所有距离,那么答案所属的路径肯定在包含青蛙1和青蛙2的最小生成树中,那么用kruskal求最小生成树,当最小生成树覆盖青蛙1和青蛙2且青蛙1和青蛙2之间连通,那么求该最小生成树的最长距离即为答案。

代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <queue>
 6 #include <vector>
 7 #include <cmath>
 8 using namespace std;
 9 #define N 205
10 
11 struct node{
12     int x, y;
13     double w;
14 }a[N*N];
15 
16 struct mem{
17     double x, y;
18 }b[N];
19 
20 int father[N];
21 int n, m;
22 double c[N];
23 
24 int findroot(int p){
25     int x=p;
26     while(x!=father[x]) x=father[x];
27     return x;
28 }
29 
30 bool cmp(node a,node b){
31     return a.w<b.w;
32 }
33 
34 void init(){
35     for(int i=0;i<=n;i++) father[i]=i;
36 }
37 
38 main()
39 {
40     int t;
41     int i, j, k1, k2, kase=1;
42     while(scanf("%d",&n)==1&&n){
43         init();
44         for(i=0;i<n;i++){
45             scanf("%lf %lf",&b[i].x,&b[i].y);
46         }
47         k1=0;
48         for(i=0;i<n;i++){
49             for(j=0;j<n;j++){
50                 a[k1].x=i;a[k1].y=j;
51                 a[k1++].w=sqrt((b[i].x-b[j].x)*(b[i].x-b[j].x)+(b[i].y-b[j].y)*(b[i].y-b[j].y));
52             }
53         }
54         sort(a,a+k1,cmp);
55         int f1, f2;
56         k2=f1=f2=0;
57         for(i=0;i<k1;i++){
58             if(f1==1&&f2==1) break;       //当青蛙0和青蛙1都在最小生成树中,且之间连通,那么就跳出 
59             int fx=findroot(a[i].x);
60             int fy=findroot(a[i].y);
61             if(fx!=fy){
62                 father[fy]=fx;
63                 c[k2++]=a[i].w;
64                 if((a[i].x==0||a[i].y==0)&&!f2||findroot(0)==findroot(1)&&f2) f1=1;     //当青蛙1没有在生成树中或者青蛙1在生成树中且青蛙0和青蛙1之间连通,那么f1就可以标记为1, 
65                 if((a[i].x==1||a[i].y==1)&&!f1||findroot(0)==findroot(1)&&f1) f2=1;     //如上 
66             }
67         }
68         sort(c,c+k2);
69         printf("Scenario #%d\nFrog Distance = %.3f\n\n",kase++,c[k2-1]);
70     }
71 }

 

POJ 2253 kruskal变形

标签:des   style   blog   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/qq1012662902/p/3966746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!