百练 / 2017计算机学科夏令营上机考试: H(Dijkstra算法)

本文介绍了一种使用Dijkstra算法求解在包含步行与地铁出行方式的城市中,从家到学校的最短时间的方法。考虑到地铁只在相邻站点间直线行驶,而人可以在任意两点间沿直线行走,通过构造全矩阵并采用deque实现候选数组来优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目来源:http://bailian.openjudge.cn/practice/2502/

2502:Subway

总时间限制: 1000ms  内存限制: 65536kB

描述

You have just moved from a quiet Waterloo neighbourhoodto 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 forclass, 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 youare lucky, and whenever you arrive at a subway station, a train is there thatyou can board immediately. You may get on and off the subway any number oftimes, and you may switch between different subway lines if you wish. Allsubway lines go in both directions.

输入

Input consists of the x,y coordinates of your home andyour school, followed by specifications of several subway lines. Each subwayline consists of the non-negative integer x,y coordinates of each stop on theline, in order. You may assume the subway runs in a straight line betweenadjacent 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 bythe dummy coordinate pair -1,-1. In total there are at most 200 subway stops inthe city.

输出

Output is the number of minutes it will take you to getto school, rounded to the nearest minute, taking the fastest route.

样例输入

0 0 10000 1000

0 200 5000 200 7000 200 -1 -1

2000 600 5000 600 10000 600 -1 -1

样例输出

21

-----------------------------------------------------

解题思路

Dijkstra最短路,但是需要自己构造全矩阵。由于是稠密阵,所以用deque实现candidate数组,复杂度为O(n2).

几个坑点:

1. 不是按水平竖直方向移动的,是按直线距离

2. 地铁只有相邻两站之间是直线,也就是说,不相邻的两站地铁之间有可能因为人可以走最短直线导致人比地铁快

-----------------------------------------------------

代码

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<limits.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;

struct node{
	int x,y;
	int line;
	double path;
	int id;
};

int Round(double a)
{
	int an = floor(a);
	double ad = a - an;
	if (ad>=0.5)
	{
		return (an+1);
	}
	else
	{
		return an;
	}
}


double walk(const node& n1, const node& n2)
{
	double l1 = abs(n1.x-n2.x);
	double l2 = abs(n1.y-n2.y);
	return 4*(sqrt(l1*l1+l2*l2));
}

double train(const node& n1, const node& n2)
{
	double l1 = abs(n1.x-n2.x);
	double l2 = abs(n1.y-n2.y);
	return sqrt(l1*l1+l2*l2);
}

int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("xly2017H.txt");
	deque<node> nvec;
	int cnt = 0;
	int line = 0;
	int id = 0;
	int x,y;
	fin >> x >> y;
	node begin;
	begin.x = x;
	begin.y = y;
	begin.line = -2;
	begin.path = 0;
	begin.id = 0;
	nvec.push_back(begin);
	cnt ++;
	fin >> x >> y;
	node end;
	end.x = x;
	end.y = y;
	end.line = -1;
	end.id = 1;
	nvec.push_back(end);
	cnt++;
	id = 2;
	while(fin >> x >> y)
	{
		if (x != -1 && y != -1)
		{
			node aNode;
			aNode.x = x;
			aNode.y = y;
			aNode.line = line;
			aNode.id = id;
			id++;
			nvec.push_back(aNode);
			cnt++;
		}
		else
		{
			line++;
		}
	}
	fin.close();
	// 构造权矩阵
	int i,j,tmpid,tmppos;
	double tmp;
	double **mat = new double*[cnt];
	for (i=0; i<cnt; i++)
	{
		mat[i] = new double[cnt]();			// 加括号:在分配内存的同时初始化为0
	}
	for (i=0; i<cnt; i++)
	{
		for (j=0; j<cnt; j++)
		{
			if (i==j)
			{
				mat[i][j] = 0;
			}
			else if (nvec.at(i).line == nvec.at(j).line && abs(i-j)==1)	// 只有相邻两站地铁才按地铁的速度走
			{
				mat[i][j] = train(nvec.at(i), nvec.at(j));
			}
			else
			{
				mat[i][j] = walk(nvec.at(i), nvec.at(j));
			}
		}
	}
	// 初始化
	nvec.pop_front();				// 弹出begin
	for (i=1; i<cnt; i++)
	{
		nvec.at(i-1).path = mat[0][i];
	}
	// Dijskra循环
	double ans = 0;
	bool is_end = false;
	for (j=0; j<cnt-2; j++)
	{
		tmp = INT_MAX;
		for (i=0; i<nvec.size(); i++)
		{
			if (nvec.at(i).path < tmp)
			{
				tmp = nvec.at(i).path;
				tmppos = i;
				tmpid = nvec.at(i).id;
			}
		}
		if(tmpid == 1)
		{
			ans = tmp;
			is_end = true;
			break;
		}

		deque<node>::iterator it = nvec.begin();
		for (i=0; i<tmppos; i++)
		{
			it++;
		}
		nvec.erase(it);
		for (i=0; i<nvec.size(); i++)
		{
			nvec.at(i).path = min(nvec.at(i).path, tmp + mat[nvec.at(i).id][tmpid]);
		}
	}
	if (!is_end)
	{
		ans = nvec.at(0).path;
	}
	ans = ans*60/4e4;
	cout << Round(ans);
	for (i=0; i<cnt; i++)
	{
		delete[] mat[i];
	}
	delete[] mat;
#endif
#ifdef ONLINE_JUDGE
	deque<node> nvec;
	int cnt = 0;
	int line = 0;
	int id = 0;
	int x,y;
	cin >> x >> y;
	node begin;
	begin.x = x;
	begin.y = y;
	begin.line = -2;
	begin.path = 0;
	begin.id = 0;
	nvec.push_back(begin);
	cnt ++;
	cin >> x >> y;
	node end;
	end.x = x;
	end.y = y;
	end.line = -1;
	end.id = 1;
	nvec.push_back(end);
	cnt++;
	id = 2;
	while(cin >> x >> y)
	{
		if (x != -1 && y != -1)
		{
			node aNode;
			aNode.x = x;
			aNode.y = y;
			aNode.line = line;
			aNode.id = id;
			id++;
			nvec.push_back(aNode);
			cnt++;
		}
		else
		{
			line++;
		}
	}
	// 构造权矩阵
	int i,j,tmpid,tmppos;
	double tmp;
	double **mat = new double*[cnt];
	for (i=0; i<cnt; i++)
	{
		mat[i] = new double[cnt]();			// 加括号:在分配内存的同时初始化为0
	}
	for (i=0; i<cnt; i++)
	{
		for (j=0; j<cnt; j++)
		{
			if (i==j)
			{
				mat[i][j] = 0;
			}
			else if (nvec.at(i).line == nvec.at(j).line && abs(i-j)==1)
			{
				mat[i][j] = train(nvec.at(i), nvec.at(j));
			}
			else
			{
				mat[i][j] = walk(nvec.at(i), nvec.at(j));
			}
		}
	}
	// 初始化
	nvec.pop_front();				// 弹出begin
	for (i=1; i<cnt; i++)
	{
		nvec.at(i-1).path = mat[0][i];
	}
	// Dijskra循环
	double ans = 0;
	bool is_end = false;
	for (j=0; j<cnt-2; j++)
	{
		tmp = INT_MAX;
		for (i=0; i<nvec.size(); i++)
		{
			if (nvec.at(i).path < tmp)
			{
				tmp = nvec.at(i).path;
				tmppos = i;
				tmpid = nvec.at(i).id;
			}
		}
		if(tmpid == 1)
		{
			ans = tmp;
			is_end = true;
			break;
		}

		deque<node>::iterator it = nvec.begin();
		for (i=0; i<tmppos; i++)
		{
			it++;
		}
		nvec.erase(it);
		for (i=0; i<nvec.size(); i++)
		{
			nvec.at(i).path = min(nvec.at(i).path, tmp + mat[nvec.at(i).id][tmpid]);
		}
	}
	if (!is_end)
	{
		ans = nvec.at(0).path;
	}
	ans = ans*60/4e4;
	cout << Round(ans);
	for (i=0; i<cnt; i++)
	{
		delete[] mat[i];
	}
	delete[] mat;
#endif
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值