POJ2253 Frogger(最短路变形,Dijkstra)

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

Source

Ulm Local 1997

思路:有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有  有两条通路  1(4)5 (3)2 代表1到5之间的边为4,  5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4,  另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4,用三种方法都能解决

代码:

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath> 
#include <cctype>
using namespace std;
#define maxn 1005
#define LL long long
int cas=1,T;
const int INF = 1<<25;
double mapp[1005][1005];
double d[maxn];
int n,m;
double dijkstra()
{
	double vis[maxn];
	int v;
	double mins;
	for (int i = 1;i<=n;i++)
	{
		d[i]=mapp[1][i];
		vis[i]=0;
	}
	d[1]=0;
	for (int i = 1;i<=n;i++)
	{
		mins = -1;
		for (int j = 1;j<=n;j++)
		{
			if (!vis[j] && d[j] > mins)
			{
				mins = d[j];
				v=j;
			}
		}
		vis[v]=1;
		for (int j = 1;j<=n;j++)
		{
			if (!vis[j] && d[j] < min(d[v],mapp[v][j]))
				d[j]=min(d[v],mapp[v][j]);
		}
	}
	return d[n];
}
int main()
{
	while (scanf("%d",&n)!=EOF)
	{
		m=n*(n-1)/2;
		memset(mapp,0,sizeof(mapp));
		double **swp;//创建二维数组 
		swp=new double*[n];
		for(int i=0;i<n;i++)
		swp[i]=new double[2];
		
		for(int i=0;i<n;i++)
		scanf("%lf%lf",&swp[i][0],&swp[i][1]);

		for(int e=0;e<n;e++)
		for(int j=e+1;j<n;j++)
		{
		double a=swp[e][0],b=swp[e][1],c=swp[j][0],d=swp[j][1];
		double t=sqrt((a-c)*(a-c)+(b-d)*(b-d));
		mapp[e+1][j+1]=mapp[j+1][e+1]=t;
	    }
		printf("Scenario #%d\n",cas++);
		int temp;
		printf("Frog Distance=%.3lf\n\n",dijkstra());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiamusansan/article/details/81137332