POJ-2253 Frogger(三种最短路和最小生成树)

POJ-2253 Frogger

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号石头所有路径中的最大跳跃距离的最小值。

思路:转化为A到B的所有路径最大值的最小值(很容易想),实现的方法就有很多了,可以用spfn和dijsktra来求,就是在松弛时变变形,数据量不大,也可以用floyd来求,也可以用最小生成树来写,用kruskal写时当1号石头与2号石头连通时就不再求了,有一段时间没写这几个算法了,就都写了写。

double f;G++提交C++提交本机gcc测试最安全的方法
输入scanf("%lf", &f);scanf("%lf", &f);scanf("%lf", &f);cin >> f;
输出printf("%f", f);printf("%lf", f);printf("%lf", f);cout << f;
这个坑要注意

Kruskal
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h> 
#include <string>
#include <vector>
#include <queue> 
#include <stack> 
#include <set>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
struct stone{
	int x;
	int y;
}s[205];
struct node{
	int u;
	int v;
	double dis;
}m[400005];
bool cmp(node n1,node n2)
{
	return n1.dis<n2.dis;
}
int f[205];
int find(int x)
{
	if(f[x]==x) return x;
	else{
		f[x]=find(f[x]);
		return f[x];
	}
}
bool merge(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	
	if(fx!=fy){
		f[fy]=fx;
		return true;
	}
	return false;
}
int main(void)
{
	int n;
	int Case=0;
	while(scanf("%d",&n)!=EOF&&n)
	{
		int count_m=0;
		for(int i=1;i<=n;i++)
		    scanf("%d%d",&s[i].x,&s[i].y);
		for(int i=1;i<=n;i++)
		    for(int j=i+1;j<=n;j++)
			{
				m[count_m].u=i;
				m[count_m].v=j;
				m[count_m++].dis=sqrt(pow((s[i].x-s[j].x),2)+pow((s[i].y-s[j].y),2));
			}
		for(int i=0;i<=n;i++)
		    f[i]=i;
		sort(m,m+count_m,cmp);
		double ans;
		
		for(int i=0;i<count_m;i++)
		{   
			if(merge(m[i].u,m[i].v))
			{
				ans=m[i].dis;
				if(find(1)==find(2))
			       break;   
			}
		}
		printf("Scenario #%d\n",++Case);
		printf("Frog Distance = %.3f\n",ans);
		printf("\n");			 
	}
	return 0;
}

Dijsktra
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h> 
#include <string>
#include <vector>
#include <queue> 
#include <stack> 
#include <set>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=201;
int head[MAXN],Count,n;
int x[MAXN],y[MAXN];
double dis[MAXN];
bool vis[MAXN];
struct EDGE{
	int v;
	int next;
	double w;
}e[MAXN*MAXN];
struct node{
	int point;
	double distance;
	node(){}
	node(int _point,double _distance){point=_point;distance=_distance;}
	bool operator<(const node&other) const
	{
		return distance>other.distance;
	}
};
void add_edge(int u,int v,double w)
{
	e[Count].v=v;
	e[Count].w=w;
	e[Count].next=head[u];
	head[u]=Count++;
}
void Dij(int s)
{
	for(int i=1;i<=n;i++)
	{
		vis[i]=false;
		dis[i]=INF;
	}
	priority_queue<node> pq;
	dis[s]=0;
	pq.push(node(s,dis[s]));
	
	while(!pq.empty())
	{
		struct node now=pq.top();
		pq.pop();
		if(vis[now.point]) continue;
		vis[now.point]=true;
		for(int i=head[now.point];i!=-1;i=e[i].next)
		{
			int to=e[i].v;
			if(dis[to]>max(dis[now.point],e[i].w))
			{
			   dis[to]=max(dis[now.point],e[i].w);
		 	   pq.push(node(to,dis[to]));
		    }
		}
		
	} 
}
int main(void)
{
	int Case=0;
	while(scanf("%d",&n)!=EOF&&n)
	{
		memset(head,-1,sizeof(head));
		Count=0;
		for(int i=1;i<=n;i++)
           scanf("%d%d",&x[i],&y[i]);
		for(int i=1;i<=n;i++)
		    for(int j=i+1;j<=n;j++)
			 {
			 	double w=sqrt(pow((x[i]-x[j]),2)+pow((y[i]-y[j]),2));
			 	add_edge(i,j,w);
			 	add_edge(j,i,w);
			 }
		Dij(1);	 
		printf("Scenario #%d\n",++Case);
		printf("Frog Distance = %.3f\n",dis[2]);
		printf("\n");    		
	}
	return 0;
}

Spfn
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h> 
#include <string>
#include <vector>
#include <queue> 
#include <stack> 
#include <set>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=201;
int head[MAXN],Count,n;
int x[MAXN],y[MAXN];
double dis[MAXN];
bool vis[MAXN];
struct EDGE{
	int v;
	int next;
	double w;
}e[MAXN*MAXN];
void add_edge(int u,int v,double w)
{
	e[Count].v=v;
	e[Count].w=w;
	e[Count].next=head[u];
	head[u]=Count++;
}
void Spfn(int s)
{
	for(int i=1;i<=n;i++)
	{
		vis[i]=false;
		dis[i]=INF;
	}
	dis[s]=0;
	vis[s]=true;
	queue<int> q;
	q.push(s);
	
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		vis[temp]=false;
		
		for(int i=head[temp];i!=-1;i=e[i].next)
		{
			int to=e[i].v;
			if(max(dis[temp],e[i].w)<dis[to])
			{
			   dis[to]=max(dis[temp],e[i].w);
			   if(!vis[to])
			   {
				  vis[to]=true;
				  q.push(to);
			   }
		    }
		}
    }
}
int main(void)
{
	int Case=0;
	while(scanf("%d",&n)!=EOF&&n)
	{
		memset(head,-1,sizeof(head));
		Count=0;
		for(int i=1;i<=n;i++)
           scanf("%d%d",&x[i],&y[i]);
		for(int i=1;i<=n;i++)
		    for(int j=i+1;j<=n;j++)
			 {
			 	double w=sqrt(pow((x[i]-x[j]),2)+pow((y[i]-y[j]),2));
			 	add_edge(i,j,w);
			 	add_edge(j,i,w);
			 }
		Spfn(1);	 
		printf("Scenario #%d\n",++Case);
		printf("Frog Distance = %.3f\n",dis[2]);
		printf("\n");    		
	}
	return 0;
}
Floyd
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h> 
#include <string>
#include <vector>
#include <queue> 
#include <stack> 
#include <set>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=201;
int x[MAXN],y[MAXN];
double map1[MAXN][MAXN];
int n;
void Floyd()
{
	for(int k=1;k<=n;k++)
	   for(int i=1;i<=n;i++)
	       for(int j=1;j<=n;j++)
	          map1[i][j]=min(map1[i][j],max(map1[i][k],map1[k][j]));
}
int main(void)
{
	int Case=0;
	while(scanf("%d",&n)!=EOF&&n)
	{
		for(int i=1;i<=n;i++)
		    scanf("%d%d",&x[i],&y[i]);
		for(int i=1;i<=n;i++)
		   for(int j=i+1;j<=n;j++)
		      map1[i][j]=map1[j][i]=sqrt(pow((x[i]-x[j]),2)+pow((y[i]-y[j]),2));	
		Floyd();
		printf("Scenario #%d\n",++Case);
		printf("Frog Distance = %.3f\n",map1[1][2]);
		printf("\n");	      
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值