POJ 2253 kruskal变形

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 }
时间: 2024-07-31 09:10:26

POJ 2253 kruskal变形的相关文章

poj 2253 dijstra变形

dijstra算法的变形,定义:dist[i]为源点到点i的若干路径上的边的最大值的最小值,然后会发现可以用和dijstra一样的贪心方法,当前dist最小的以后都不会再被更新. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 7 const int INF = 9999999; 8 const

poj 2253(kruskal)

Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34968   Accepted: 11235 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,

Floyd-Warshall算法(求解任意两点间的最短路) 详解 + 变形 之 poj 2253 Frogger

/* 好久没有做有关图论的题了,复习一下. --------------------------------------------------------- 任意两点间的最短路(Floyd-Warshall算法) 动态规划: dp[k][i][j] := 节点i可以通过编号1,2...k的节点到达j节点的最短路径. 使用1,2...k的节点,可以分为以下两种情况来讨论: (1)i到j的最短路正好经过节点k一次 dp[k-1][i][k] + dp[k-1][k][j] (2)i到j的最短路完全

POJ 2253 Frogger

题意:一只青蛙找到另外一只青蛙,不过可以通过其它的石头跳到目标青蛙的位置去,其中,输入数据的时候第一组数据是第一只青蛙的位置,第二组是目标青蛙的位置,其它的为石头的位置 思路:dijkstra算法的一种小小的变形,做法还是一样的 Tips:POJ上的双精度浮点型输出竟然是%f输出害的我一直错,或者是编译错误,恼啊! AC代码: #include<cstdio> #include<cmath> #include<algorithm> using namespace std

poj 2253 (dis最短路径)

Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24979   Accepted: 8114 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,

poj 2253 Frogger (最长路中的最短路)

链接:poj 2253 题意:给出青蛙A,B和若干石头的坐标,现青蛙A想到青蛙B那,A可通过任意石头到达B, 问从A到B多条路径中的最长边中的最短距离 分析:这题是最短路的变形,以前求的是路径总长的最小值,而此题是通路中最长边的最小值,每条边的权值可以通过坐标算出,因为是单源起点,直接用SPFA算法或dijkstra算法就可以了 SPFA 16MS #include<cstdio> #include<queue> #include<cmath> #include<

[2016-04-02][POJ][2253][Frogger]

时间:2016-04-02 17:55:33 星期六 题目编号:[2016-04-02][POJ][2253][Frogger] 题目大意:给定n个点的坐标,问从起点到终点的所有路径中,最大边的最小值是多少,即每一步至少是多少才能走到终点 分析: 方法1: 枚举出完全图,然后从起点跑一次Dijkstra算法,不过选点不再是选择起点到终点路径的点,而是起点到终点的路径中,边最大边最小的点,即d数组保存起点到当前点的路径中最大边的最小值, 最大边的最小值:u->v d[v] = min(d[i],m

poj 2253

群 #include <stdio.h>#include <math.h> int a[220][220];int x[220],y[220],n;int init(){ int i,j; for(i=0;i<n;i++) scanf("%d%d",&x[i],&y[i]); for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])

POJ 2253 - Frogger (floyd)

A - Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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