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

本文介绍了一个基于Dijkstra算法的应用案例,旨在解决城市中利用不同交通方式寻找从家到学校的最短时间路径问题。通过计算步行与乘坐地铁的时间,并考虑地铁线路的实际情况,实现了综合最优路径的计算。

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 模板;
基于模拟退火的计算器 在线运行 访问run.bcjh.xyz。 先展示下效果 https://pan.quark.cn/s/cc95c98c3760 参见此仓库。 使用方法(本地安装包) 前往Releases · hjenryin/BCJH-Metropolis下载新 ,解压后输入游戏内校验码即可使用。 配置厨具 已在2.0.0弃用。 直接使用白菜菊花代码,保留高级厨具,新手池厨具可变。 更改迭代次数 如有需要,可以更改 中39行的数字来设置迭代次数。 本地编译 如果在windows平台,需要使用MSBuild编译,并将 改为ANSI编码。 如有条件,强烈建议这种本地运行(运行可加速、可多次重复)。 在 下运行 ,是游戏中的白菜菊花校验码。 编译、运行: - 在根目录新建 文件夹并 至build - - 使用 (linux) 或 (windows) 运行。 后在命令行就可以得到输出结果了! (注意顺序)(得到厨师-技法,表示对应新手池厨具) 注:linux下不支持多任务选择 云端编译已在2.0.0弃用。 局限性 已知的问题: - 无法得到优解! 只能得到一个比较好的解,有助于开阔思路。 - 无法选择菜品数量(默认拉满)。 可能有一定门槛。 (这可能有助于防止这类辅助工具的滥用导致分数膨胀? )(你问我为什么不用其他语言写? python一个晚上就写好了,结果因为有涉及json读写很多类型没法推断,jit用不了,算这个太慢了,所以就用c++写了) 工作原理 采用两层模拟退火来大化总能量。 第一层为三个厨师,其能量用第二层模拟退火来估计。 也就是说,这套方法理论上也能算厨神(只要能够在非常快的时间内,算出一个厨神面板的得分),但是加上厨神的食材限制工作量有点大……以后再说吧。 (...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值