POJ - 1135 自己构造图,最短路dijkstar模板求解

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school. 
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
Input
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Sample Input
0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1
Sample Output
21


题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时间(分钟)


求解:只要是地铁相邻的站就用40km/h的速度求解,其他的都用10km/h的求解;

代码:

#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#include<math.h>
#define Max 500
const double pi = 9999999.00;
struct node
{
	double x,y;
}stu[1100];


double e[Max][Max];
double dis[Max];
int book[Max];

void init()
{
	for(int i=0;i<=300;i++)
		for(int j=0;j<=300;j++)
		{
			if(i==j) e[i][j] = 0;
			else e[i][j] = pi;
		}
}

void dijkstar(int s,int n)
{
	memset(book,0,sizeof(book));
	int i,j;
	for(i=1;i<=n;i++)
		dis[i]=e[s][i];
	book[s]=1;
	
	for(i=1;i<=n-1;i++)
	{
		double tt=pi;
		for(j=1;j<=n;j++)
		{
			if(!book[j]&&tt>dis[j])
			{
				tt=dis[j];
				s=j;
			}
		}
		book[s]=1;
		for(j=1;j<=n;j++)
		{
			if(!book[j]&&e[s][j]<pi&&dis[j]>dis[s]+e[s][j])
			{
				dis[j]=dis[s]+e[s][j];
			}
		}	
	}
}




int main()
{
	int i,j,k;
	while(~scanf("%lf%lf%lf%lf",&stu[1].x,&stu[1].y,&stu[2].x,&stu[2].y))
	{
		double x,y;
		int tt=3,f=0;
		init();
		while(~scanf("%lf%lf",&x,&y))
		{
			if(x==-1&&y==-1)
			{
				f=0;    // f=0 一定要放上面;让我改了一上午的错,原来是f=0放continue
						//下面了,以后一定要认真打代码了; 
				continue;
				
			}		
			stu[tt].x=x;
			stu[tt].y=y;
			f++;
			if(f>=2)
			{
				k=tt-1;
				double ss=(stu[tt].x-stu[k].x)*(stu[tt].x-stu[k].x)+(stu[tt].y-stu[k].y)*(stu[tt].y-stu[k].y);
				double q=(sqrt(ss))/40000.0;
				e[k][tt]=q;
				e[tt][k]=q;
			
			}
			tt++;
		}
		for(i=1;i<tt;i++)
		{
			for(j=1;j<tt;j++)
			{
				if(i!=j&&e[i][j]==pi)
				{
					double ss=(stu[i].x-stu[j].x)*(stu[i].x-stu[j].x)+(stu[i].y-stu[j].y)*(stu[i].y-stu[j].y);
					double q=(sqrt(ss))/10000.0;
					e[i][j]=q;
					e[j][i]=q;	
					//printf("e[%d][%d]==%lf\n",i,j,q);
				}
			}
		}
		
		dijkstar(1,tt-1);
		
		printf("%.0lf\n",dis[2]*60);
	}
	return 0;
}

dijkstar 模板;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值