1. 程式人生 > >2502 Subway(哎,這題建圖好煩啊)

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

          煩啊,這題其實就是建圖來一個spfa,可是是真的麻煩。。。

#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<utility>
#include<algorithm>
#include<cmath>
using namespace std;
struct fuck
{
	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();
	}
}