poj2253(dijstra应用)

This blog explores a problem where Freddy Frog aims to find the shortest path to reach his beloved Fiona Frog by hopping on stones. Each stone's coordinates are given, and the goal is to calculate the maximum distance Freddy needs to jump to reach Fiona. The solution involves implementing Dijkstra's algorithm for finding the shortest path in a graph.

      题目链接:http://poj.org/problem?id=2253

    题目意思:Freddy Frog暗恋Fiona Frog,在他们之间有n快石头,告诉你这n快石头的坐标,第一快为Freddy Frog的坐标,第n块为Finoa Frog的坐标,Freddy可以借助石头经过任何路径到达Fiona那里,问他最小的弹跳距离是多少(即最短路径中的最长边)。

    注意:该题有多组测试数据,要求每组测试数据之间空一行!

 

代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
struct coo{
   double x;
   double y;
}st[205];
double map[205][205];
int flag[205];
double dis[205];
int N;
double dist(int i,int j)
{
	double x1,y1;
	x1=pow(st[j].x-st[i].x,2.0);
	y1=pow(st[j].y-st[i].y,2.0);
	return sqrt(x1+y1);
}
double max(double a,double b)
{
	return a>b?a:b;
}
void dijkstra()
{
	int i,j,u;
	double min1;
    dis[0]=0;
	for(i=1;i<N;i++)
	{
		dis[i]=map[0][i];
		flag[i]=0;
	}
	flag[0]=1;
	for(i=1;i<N;i++)
	{
		min1=10000000;
		for(j=1;j<N;j++)
		{
			if(flag[j]==0 && dis[j]<min1)
			{
				u=j;
				min1=dis[j];
			}
		}
		if(u==1)
			break;
		flag[u]=1;
		{
			for(j=1;j<N;j++)
			{
				if(flag[j]==0 && dis[j]>max(dis[u],map[u][j]))
				{
                       dis[j]=max(dis[u],map[u][j]);
				}
			}
		}
	}
}
void main()
{
     int i,j,k;
	 int Case=0;
	
	 while(scanf("%d",&N)!=EOF && N!=0)
	 {
		 Case++;
	    for(i=0;i<N;i++)
	    	scanf("%lf%lf",&st[i].x,&st[i].y);
		memset(map,0,sizeof(map));
		k=0;
		for(i=0;i<N;i++)
		{
			for(j=k;j<N;j++)
			{
              map[i][j]=map[j][i]=dist(i,j);
			}
             k++;
		}
		/*for(i=0;i<N;i++)
		{
			for(j=0;j<N;j++)
				printf("%.3lf ",map[i][j]);
			printf("\n");
		}*/
		dijkstra();
		
		printf("Scenario #%d\nFrog Distance = %.3lf\n\n",Case,dis[1]);
	 }
}


 

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值