POJ 2502 Subway

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
题意:求从家到学校花费的最少时间,途中可以坐地铁(40km/h),也可以步行(10km/h),最后输出话费多少分钟。
第一行输入两个坐标,第一个坐标代表家的位置(x1,y1);第二个位置代表学校的位置(x2,y2)                            接下来代表一个地铁线:(x1,y1),(x2,y2),...,(x3,y3),直到(-1,-1)结束,表示这是若干地铁站点。
思路:主要考察的就是构图了,我们可以把坐地铁花费的时间一一求出来,如果两个地铁站相连,那么就把它的时间求出来,可以分钟为单位(因为最后是输出分钟),因为最多只有200个点,这就转换成的普通的最短路问题,我们把家当成初始点,把学校当成结束点,中间点与点的距离,我们可以用两点坐标公式求出来,如果这两点是可以坐地铁,就比上地铁速度,如果不可以坐地铁,就比上步行速度,具体请看代码:
#include <math.h>
#include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f
int book[205];
double dis[205];
double e[205][205];
struct node{
	int u, v;
	void Node(int x, int y) {
		u = x; v = y;
	}
}s[205];
double Sqrt(node s1, node s2) {
	return sqrt((s1.u - s2.u*1.0)*(s1.u - s2.u) + (s1.v - s2.v*1.0)*(s1.v - s2.v));
}
void Dijkstra(int n) {
	dis[1] = 0;
	book[1] = 1;
	for(int i = 1; i <= n; i++) {
		bool flag = false;
		int p;
		double mn = inf;
		for(int j = 1; j <= n; j++) {
			if(!book[j] && mn > dis[j]) {
				mn = dis[j];
				p = j;
				flag = true;
			}
		}
		if(!flag) break;
		book[p] = 1;
		for(int j = 1; j <= n; j++) {
			if(book[j]) continue;
			if(dis[j] > (dis[p] + e[p][j])) {
				dis[j] = dis[p] + e[p][j];
			}
		}
	}
}
int main() {
	int t, p;
	int u, v;
	int x1, y1, x2, y2;
	scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
	t = 1;
	p = 2;
	s[t++].Node(x1, y1);
	while(~scanf("%d %d", &u, &v)) {
		if(u == -1 && v == -1) {
			for(int i = p; i < t-1; i++) {
				e[i+1][i] = e[i][i+1] = Sqrt(s[i], s[i+1])*60/40000.0;//坐地铁时间 
			}
			p = t;//记录地铁线 
			continue;//不再执行下步操作 
		}
		s[t++].Node(u, v);
	}
	s[t].Node(x2, y2);
	for(int i = 1; i <= t; i++) {
		for(int j = 1; j <= t; j++) {
			if(e[i][j] == 0) {
				e[i][j] = e[j][i] = Sqrt(s[i], s[j])*60/10000.0;//步行时间 
			}
		}
	}
	for(int i = 1; i <= t; i++) {
		dis[i] = e[1][i];
	}
	Dijkstra(t);
	printf("%d\n",(int)(dis[t] + 0.5));//注意精度损失 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值