POJ - 2502 Subway(哎,这题建图好烦啊)

                                                                                Subway

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13146   Accepted: 4265

Description

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

Source

Waterloo local 2001.09.22

 

          烦啊,这题其实就是建图来一个spfa,可是是真的麻烦。。。

 

#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<utility>
#include<algorithm>
#include<cmath>
using namespace std;
struct ***
{
	int to, ne;
	double len;
}ed[1000005];
int head[1005];
int vis[1005];
double d[1005];
int x[1005], y[1005]; 
int n, cnt;
map<pair<int, int> ,int>na;
void add(int from, int to, double len)
{
	ed[cnt].to = to;
	ed[cnt].len = len;
	ed[cnt].ne = head[from];
	head[from] = cnt++;
}
void init()
{
	for (int s = 1; s <= 1000; s++)
	{
		vis[s] = 0;
		d[s] = 0x3f3f3f3f;
		head[s] = -1;
	}
	cnt = 0;
	n = 1;
}
void spfa()
{
	d[1] = 0;
	vis[1] = 1;
	queue<int>q;
	q.push(1);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		vis[t] = 0;
		for (int s = head[t]; ~s; s = ed[s].ne)
		{
			if (d[ed[s].to] > d[t] + ed[s].len)
			{
				d[ed[s].to] = d[t] + ed[s].len;
				if (!vis[ed[s].to])
				{
					vis[ed[s].to] = 1;
					q.push(ed[s].to);
				}
			}
		}
	}
	printf("%.0f\n", d[2]);
}
int main()
{
	int a, b, c, d, na1, na2;
	while (~scanf("%d%d", &a, &b))
	{
		init();
		x[n] = a; y[n] = b;
		na[make_pair(a, b)] = n++;
		scanf("%d%d", &x[n], &y[n]);
		na[make_pair(x[2], y[2])] = n++;
		while (scanf("%d%d", &a, &b)==2)
		{
			if (!na[make_pair(a, b)])
			{
				x[n] = a, y[n] = b;
				na[make_pair(a, b)] = n++;
			}
			na1 = na[make_pair(a, b)];
			while(scanf("%d%d", &c, &d)!=EOF &&c != -1 && d != -1)
			{
				double len = 60*sqrt(((a - c)*(a - c) + (b - d)*(b - d))*1.0)/40000.0;
				if (!na[make_pair(c, d)])
				{
					x[n] = c; y[n] = d;
					na[make_pair(c, d)] = n++;
				}
				na2 = na[make_pair(c, d)];
				add(na1, na2, len);
				add(na2, na1, len);
				na1 = na2;
				a = c;
				b = d;
			}
		}		
		for (int s = 1; s < n - 1; s++)
		{
			for (int w = s + 1; w < n; w++)
			{
				double len = 60*sqrt(((x[s] - x[w])*(x[s] - x[w]) + (y[s] - y[w])*(y[s] - y[w]))*1.0) / 10000.0;
				add(s, w, len);
				add(w, s, len);
			}
		}
		spfa();
	}
}

 

全部评论

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务