Subway POJ - 2502 最短路径(dijkstra算法)

  • Subway

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

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。输入的规则是第一行输入的是x1,y1,x2,y2,分别代表家的坐标和学校的坐标。以后输入的是车站的坐标,数目不超过200,相邻的两个站点可以坐地铁,其他的需要步行。问到达学校的最短时间是多少?

思路:这题主要麻烦的一个是建图,还一个是精度问题,输入时注意要用while (scanf(.....) != EOF),建好图后就是简单的最短路径问题,可以直接套dijkstra算法模板。注意:同一条地铁线路只能一站一站坐不然就要步行过去。比如线路1-2-3,1-2可以坐地铁,但是1-3就要步行才能到达。输出要求输出整型,注意精度。

网上有人说用printf(“%d\n”,(int)(dist[a]+0.5)),但实际上直接printf("%.0f",dist[a])也可以。

不知道为什么只改最后的输出形式,时间差了31ms。

这题原来一直错在计算时间的函数写法上,本来还以为是输出精度的问题。

一开始写成这种形式,直接在函数中判断得出结果。但是一直wa。

double dist_cal(int a, int b, int c, int d, int ct) {
	if (!ct)
		return sqrt((a - c)*(a - c) + (b - d)*(b - d))*3.0 / 500.0;
	else
		return sqrt((a - c)*(a - c) + (b - d)*(b - d))*3.0 / 2000.0;
}

后来换成这样的,然后在建图的时候计算时间,就可以过。可能也是精度问题。

double dist_cal(int a, int b, int c, int d) {
	return sqrt((double)(a - c)*(a - c) + (double)(b - d)*(b - d));
}

AC代码:

16ms

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#define ll long long
const int INF = 0x3f3f3f3f;
const int MAXN = 100000 + 5;
using namespace std;
int n, m, z, t;
double dist[MAXN];
int visit[MAXN];
double map[210][210];
int arr[210][2];
int cnt = 2; //记录一共有几个点
double dist_cal(int a, int b, int c, int d);
void dijkstra(int x);
int main() {
	scanf("%d%d%d%d", &n, &m, &z, &t);
	for (int i = 1; i <= 205; ++i) {
		for (int j = 1; j <= 205; ++j) {
			if (i == j)
				map[i][j] = 0.0;
			else
				map[i][j] = INF*1.0;
		}
	}
	arr[1][0] = n;
	arr[1][1] = m;
	arr[2][0] = z;
	arr[2][1] = t;
	int a, b;
	bool flag = true;
	while (scanf("%d%d", &a, &b) != EOF) {
		if (a == -1 && b == -1) {
			flag = true;
			continue;
		}
		else {
			cnt++;
			if (!flag) {
				arr[cnt][0] = a;
				arr[cnt][1] = b;
				if (map[cnt - 1][cnt] > dist_cal(arr[cnt - 1][0], arr[cnt - 1][1], arr[cnt][0], arr[cnt][1]))
					map[cnt - 1][cnt] = map[cnt][cnt - 1] = dist_cal(arr[cnt - 1][0], arr[cnt - 1][1], arr[cnt][0], arr[cnt][1])*3.0 / 2000.0;
			}
			else {
				flag = false;
				arr[cnt][0] = a;
				arr[cnt][1] = b;
			}
		}
	}
	for (int i = 1; i <= cnt; ++i) {
		for (int j = i+1; j <= cnt; ++j) {
			if (map[i][j] > dist_cal(arr[i][0], arr[i][1], arr[j][0], arr[j][1]))
				map[j][i] = map[i][j] = dist_cal(arr[i][0], arr[i][1], arr[j][0], arr[j][1])*3.0 / 500.0;
		}
	}
	dijkstra(1);
	//printf("%.0f\n", dist[2]);
	printf("%d\n", (int)(dist[2]+0.5));
	return 0;
}

double dist_cal(int a, int b, int c, int d) {
	return sqrt((double)(a - c)*(a - c) + (double)(b - d)*(b - d));
}

void dijkstra(int x) {
	memset(visit, 0, sizeof(visit));
	for (int i = 1; i <= cnt; ++i) {
		dist[i] = map[x][i];
	}
	for (int i = 1; i <= cnt; ++i) {
		int a = 1;
		double minn = INF;
		for (int j = 1; j <= cnt; ++j) {
			if (!visit[j] && dist[j] < minn) {
				a = j;
				minn = dist[j];
			}
		}
		visit[a] = 1;
		for (int j = 1; j <= cnt; ++j) {
			if (!visit[j] && dist[j]>dist[a] + map[a][j])
				dist[j] = dist[a] + map[a][j];
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43821265/article/details/86763432