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

poj 2253 Frogger【最小生成树变形】【kruskal】

时间:2015-08-13 14:07:45      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30427   Accepted: 9806

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个点的坐标,连通第一个点和第二个点有很多条路径,每条路径都会有 一个最大距离d - ->(当前路径经过的点集里面 任意两点间的距离都小于或者等于d)。题目要求找到这样的一个路径:(1)连通1,2两点;(2)这条路径中的最大距离d 是所有可选择路径中最小的。  输出这条路径的最大距离d。

题解:利用并查集kruskal算法;先将任意两个点之间的距离按从小到大的顺序排列,然后开始枚举所有的边,直至找到使起点和终点联通的边;
因为要求所有可以连通的路径的最大权值中最小的,所以一旦找到使其连通的边就是所要求的结果
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 210
#define DD double
using namespace std;
int set[MAX];
DD a[MAX],b[MAX];
int k,t,n;
struct node
{
	int u,v;
	DD w;
}s[50000];
bool cmp(node a,node b)
{
	return a.w<b.w;
}
DD dis(DD x1,DD y1,DD x2,DD y2)
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void init()
{
	for(int i=0;i<=n;i++)
	    set[i]=i;
}
void getmap()
{
	for(int i=0;i<n;i++)
    	scanf("%lf%lf",&a[i],&b[i]);
    k=0;
    for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
        {
        	s[k].u=i;
        	s[k].v=j;
        	s[k++].w=dis(a[i],b[i],a[j],b[j]);
		}
	sort(s,s+k,cmp);
}
int find(int fa)
{
	if(fa==set[fa])
	    return fa;
	return set[fa]=find(set[fa]);
}
void mix(int x,int y)
{
	int fx,fy;
	fx=find(x);
	fy=find(y);
	if(fx!=fy)
	    set[fx]=fy;
}
void solve()
{
	int i,j;
	int ok;
	DD ant;
	for(i=0;i<k;i++)
	{
		ok=0;
		ant=s[i].w;
		mix(s[i].u,s[i].v);
		if(find(0)==find(1))//判断是否连通 
			ok=1;
		if(ok)//第一次连通时就是最小的 
		{
		    printf("Scenario #%d\n",++t);
		    printf("Frog Distance = %.3f\n\n",ant);
		    return ;
        }
	}
}
int main()
{
	t=0;
	while(scanf("%d",&n),n)
	{
		init();
		getmap();
		solve();
	}
	return 0;
}

  

poj 2253 Frogger【最小生成树变形】【kruskal】

标签:

原文地址:http://www.cnblogs.com/tonghao/p/4726715.html

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