Transit search |
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 39 Accepted Submission(s): 18 |
Problem Description
Henry decides to develop a web site, which will provide the service of transit search. But he can only get the transit data of Guangzhou, so his web site can only support the transit search of Guangzhou. We suppose Guangzhou is 10240 meters by 10240 meters. The coordinate of the top-left corner is (0,0). The coordinate of the bottom-right corner is (10240,10240). The X–axis is from top to bottom and the Y-axis is from left to right. At the beginning, four pictures of the size 10cm by 10 cm make up of the whole map of Guangzhou. They are numbered from 0 to 3. It is to say at the beginning the scale of the map is 1cm:512 meters. We call the four pictures are at level 1.
![]() ![]() |
Input
The input begins with a line containing an integer T, the number of test cases. For each case, the first two lines describe the start position and the end position as followed. Start sx sy End ex ey Start and End both contain 8 characters each from 0 to 3. 0cm<=sx,sy,ex,ey<=10cm. Notice that all the numbers in the input are integers. The next line contains an integer n(0<n<5001), indicating the total number of bus stops in Guangzhou. The following n lines each describe a bus stop in the format: Name x y Name contains no more than 20 characters. 0<=x,y<=10240. Next comes an integer m(0<m<=100), indicating the number of bus lines in Guangzhou. Then following is the description of the m bus lines. Each bus line is described as followed: K Name1 Name2 Name3 … Namek K(0<K<=30) is the number of bus stops along the bus line. Namei is the ith bus stop along the bus line. Notice that the bus line is bidirectional.
|
Output
(1) If the distance between the start position and end position is no more than 2000 meters, print “walk there” in a single line. (2) If you can take buses according to the above rule, print the fewest number of buses you have to take. For example, if you can take a bus directly to end position without changing bus line, print 1. (3) Otherwise, print “take a taxi” in a single line.
|
Sample Input
3 00000000 1 1 00001000 3 3 4 a 1 1 b 20 30 c 40 50 d 100 100 2 3 a b c 3 b c d 00000000 1 1 03231130 5 5 5 a 1 1 b 1000 1000 c 3000 3000 d 3000 4000 e 4500 4000 2 3 a b c 3 c d e 00000000 1 1 03231130 5 5 4 a 1 1 b 1000 1000 c 3000 3000 d 3000 4000 2 3 a b c 3 b c d |
Sample Output
walk there 2 take a taxi |
Source
2008 Asia Regional Chengdu
|
Recommend
lcy
|
分析:先转化成全局坐标,然后找到起点和终点的范围内的所有车站,最短路。
#include<cstdio> #include<cstring> #include<iostream> #include<iostream> #include<queue> #include<map> #include<algorithm> #include<string> using namespace std; #define maxn 0x7fffffff typedef pair<int, int> pii; char st[10], name[25]; int len[8] = {5120, 2560, 1280, 640, 320, 160, 80, 40}; int k, end[110], vis[110]; int d[110], pos[5010][110], num[5010]; int stationstart[5010], stationend[5010]; int cnt; typedef struct S { int v; struct S *next; } EDGE; EDGE *node[110], edge[10010]; void add(int a, int b) { edge[cnt].v = b; edge[cnt].next = node[a]; node[a] = &edge[cnt++]; } inline int fun(int a, int b, int c, int d) { if (a - c <= 1000 && a - c >= -1000 && b - d <= 1000 && b - d >= -1000) { if ((a - c)*(a - c)+(b - d)*(b - d) <= 1000000) { return 1; } } return 0; } int main() { int i, j, T, x, y, a, b, n, m, sx, sy, ex, ey, t, p, ok, walk; pii U; scanf("%d", &T); while (T--) { scanf("%s%d%d", st, &a, &b); x = y = 0; for (i = 0; i < 8; ++i) { if (st[i] == '1') y += len[i]; else if (st[i] == '2') x += len[i]; else if (st[i] == '3') { x += len[i]; y += len[i]; } } sx = x + a * 4; sy = y + b * 4; scanf("%s%d%d", st, &a, &b); x = y = 0; for (i = 0; i < 8; ++i) { if (st[i] == '1') y += len[i]; else if (st[i] == '2') x += len[i]; else if (st[i] == '3') { x += len[i]; y += len[i]; } } ex = x + a * 4; ey = y + b * 4; // printf("start and end %d %d %d %d\n",sx,sy,ex,ey); map <string, int> mymap; scanf("%d", &n); memset(stationstart, 0, sizeof (stationstart)); memset(stationend, 0, sizeof (stationend)); for (i = 0; i < n; ++i) { scanf("%s%d%d", name, &x, &y); mymap[name] = i; if (fun(sx, sy, x, y)) { stationstart[i] = 1; // printf("startok=%d\n",i); } if (fun(ex, ey, x, y)) { stationend[i] = 1; // printf("endpk=%d\n",i); } } scanf("%d", &m); memset(end, 0, sizeof (end)); memset(vis, 0, sizeof (vis)); memset(num, 0, sizeof (num)); priority_queue<pii, vector<pii>, greater<pii> >q; for (i = 0; i < m; ++i) { d[i] = maxn; node[i] = NULL; scanf("%d", &k); for (j = 0; j < k; ++j) { scanf("%s", name); t = mymap[name]; if (stationstart[t]) { d[i] = 1; q.push(make_pair(1, i)); } if (stationend[t]) { end[i] = 1; } pos[t][num[t]++] = i; } } if (sx - ex <= 2000 && sx - ex >= -2000 && sy - ey <= 2000 && sy - ey >= -2000) { if ((sx - ex)*(sx - ex)+(sy - ey)*(sy - ey) <= 4000000) { walk = 1; printf("walk there\n"); continue; } } for (i = 0; i < n; ++i) { for (j = 0; j < num[i]; ++j) { for (p = 0; p < num[i]; ++p) { if (j == p) continue; add(pos[i][j], pos[i][p]); } } } ok = 0; while (!q.empty()) { U = q.top(); q.pop(); a = U.second; if (end[a]) { ok = 1; break; } vis[a] = 1; EDGE *temp = node[a]; for (; temp; temp = temp->next) { b = temp->v; if (!vis[b] && d[a] + 1 < d[b]) { d[b] = d[a] + 1; q.push(make_pair(d[b], b)); } } } if (ok) { printf("%d\n", d[a]); } else printf("take a taxi\n"); } return 0; }