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

poj 2253 Frogger

时间:2015-08-13 22:23:52      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:poj2253   frogger   最小生成树   

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30438   Accepted: 9810

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

题意:有两只青蛙一个在1处,一个在2处,求出1和2所在的最小树中最大的边!
代码:
prim:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAX 0x3f3f3f3f
using namespace std;
int n,cot=0;
double map[250][250];
int prim()
{
	double lowcost[100000],min;
	double w[100000];
	int i,j,mark;
	for(i=2;i<=n;i++)
	lowcost[i]=map[1][i];
	lowcost[1]=0;
	int t=0;
	for(i=2;i<=n;i++)
	{
		min=MAX;
		for(j=2;j<=n;j++)//寻找最短距离 
		{
			if(lowcost[j]<min&&lowcost[j]!=0)
			{
				min=lowcost[j];
				mark=j;
			}
		}
		lowcost[mark]=0;
		w[t++]=min;
		if(mark==2)//当1和2连在一个树上时,树的最大的边即为要求 
		{
			sort(w,w+t);//排序求最大的边 
		break;
	    }
		for(j=2;j<=n;j++)
		{
			if(map[mark][j]<lowcost[j])
			lowcost[j]=map[mark][j];
		}
	}
	return printf("Scenario #%d\nFrog Distance = %.3f\n\n",cot,w[t-1]);
}
int main()
{
	int x[250],y[250],i,j;
	while(scanf("%d",&n),n)
	{
		cot++;
		memset(map,0,sizeof(map));
		for(i=1;i<=n;i++)
			scanf("%d%d",&x[i],&y[i]);
		for(i=1;i<=n;i++) 
		{
			for(j=i;j<=n;j++)
			{
				double dis=sqrt((x[i]-x[j])*1.0*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
				map[i][j]=map[j][i]=dis;
			}
		}
		prim();
	}
return 0;
}
克鲁斯卡尔:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<math.h>
int n,set[250];
double x[250],y[250];

struct line
{
	int bg;
	int ed;
	double dis;
}num[250000];
int cmp(line a,line b)//排序 
{
	return a.dis <b.dis  ;
}
double d(int i,int j)
{
	double c;
	return c=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int find(int p)//寻找距离最小的点 
{
	int t;
	int child=p;
	while(p!=set[p])
	p=set[p];
	while(child!=p)
	{
		t=set[child];
		set[child]=p;
		child=t;
	}
	return p;
}
int merge(int x,int y)//合并 
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		set[fx]=fy;
		return 1;
	}
	return 0;
}
int main()
{
	int i,j;
	int v=1;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<=n;i++)
		 {
		 	set[i]=i;
		 }
		for(i=1;i<=n;i++)
		   {
		   	scanf("%lf%lf",&x[i],&y[i]);
		   }
		   int t=0;
		   	for(i=1;i<=n;i++)
		      for(j=1;j<=n;j++)
		  {
		     num[t].bg =i;
		     num[t].ed =j;
		     num[t].dis =d(i,j);
		     t++;
		  }
		  sort(num,num+t,cmp);
		  double  mxe;
		  for(i=0,j=0;i<t;i++)
		   {
		   	if(merge(num[i].bg ,num[i].ed ))
		   	{
		   		if(find(1)==find(2))//当1和2连在一个树上时,树的最大的边即为要求 
		   		{

                mxe=num[i].dis ;
                break;
				}
			}
		   }
		   if(v)
		   printf("Scenario #%d\n",v++);
		   printf("Frog Distance = %.3f\n\n",mxe );
	}	
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 2253 Frogger

标签:poj2253   frogger   最小生成树   

原文地址:http://blog.csdn.net/zhangxiaoxiang123/article/details/47618299

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