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

题目来源: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
#include
#include
#include
#include
#include
#include
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 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::iterator it = nvec.begin();
		for (i=0; i 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::iterator it = nvec.begin();
		for (i=0; i


你可能感兴趣的:(百练OJ/poj,基础算法)