1131. Subway Map (a)Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Standard author CHEN, Yue
In the "big cities", the subway systems always look and so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now, are supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest way to his/her.
Input Specification:
Each input file contains one test case. For each case, the contains a positive integer N (< =100), the number of subway lines. Then n lines follow, with the i-th (i = 1, ..., N) line describes the I-th subway line in the format:
M s[1] s[2] ... S[M]
where M (<=) is the number of stops, and s[i]' S (i = 1, ...) M) are the indices of the stations (the indices are 4-digit numbers to 0000) 9999 the line. It is guaranteed this stations are given in the correct order--this is, the train travels between-s[i ] and S[i+1] (i = 1, ..., M-1) without any stop.
Note:it is possible to have loops, but not self-loop (no train starts from s and stops at S without passing through Anoth Er station). Each station interval belongs to a unique subway line. Although the lines may cross the stations of "called Transfer" (so stations). N of more than 5 lines.
After the description of the subway, another positive an integer K (<=) is given. Then K lines Follow, each gives a query from your user:the two indices as the starting station and the destination Ctively.
The following figure shows the sample map.
Note:it is guaranteed this all the stations are reachable, and the queries consist of legal station.
Output Specification:
For each query, the minimum number of stops. Then you are supposed to show the optimal path in a friendly format as the following:
Take line#x1 from S1 to S2.
Take line#x2 from S2 to S3.
......
where Xi' s are the line numbers and Si' are the station indices. Note:besides the starting and ending stations, only the transfer stations is shall.
If the quickest path is not unique, output the "one with" minimum number of transfers, which is guaranteed to be unique. Sample Input:
4
7 1001 3212 1003 1204 1005 1306 7797 9 9988 2333 1204 2006 2003
2302 2001
13 3011 3812 3013 3001 1 306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
3
3011 3013 6666 2001
2004 3001
Sample Output:
2
Take line#3 from 3011 to 3013.
Ten
Take line#4 from 6666 to 1306.
Take line#3 from 1306 to 2302.
Take line#2 from 2302 to 2001.
6
Take line#2 from the to 1204.
Take line#1 from 1204 to 1306.
Take line#3 from 1306 to 3001.
and TM shortest way, this problem God vexed, this is the first BFS determine the shortest, and then DFS to find the smallest output, the reason may be timed out, but Pat's data is not strong can casually.
#include <map> #include <set> #include <ctime> #include <cmath> #include <stack> #inc lude<queue> #include <string> #include <vector> #include <cstdio> #include <cs tring> #include <iostream> #include <algorithm> #include <functional> using NAMESP
Ace STD; #define MS (X,y) memset (x,y,sizeof (x)) #define REP (i,j,k) for (int i=j;i<=k;i++) #define/(I,J,K) for (i
NT i=j;i>=k;i--) #define LOOP (I,J,K) for (int i=j;i!=-1;i=k[i]) #define INONE (x) scanf ("%d", &x)
#define INTWO (X,y) scanf ("%d%d", &x,&y) #define INTHR (x,y,z) scanf ("%d%d%d", &x,&y,&z) #define Infou (x,y,z,p) scanf ("%d%d%d%d", &x,&y,&z,&p) #define Lson x<<1,l,mid #define Rson X<<1|1,mid+1,r #define MP (I,J) Make_pair (i,j) #define FF the #define SS-second typedef long LONG
LL; typedef PAIR<int, int> PII;
const int low (int x) {return x&-x;} const int INF = 0x7fffffff;
const INT mod = 1e9 + 7;
const int N = 1e5 + 50;
Const double EPS = 1e-10;
int T, N, m, x, Y, d[n];
int ft[n], nt[n], u[n], v[n], SZ, now;
PII A[n], b[n];
void Dfs (int x, int y, int z, int sz, int g) {if (x = = y) {if (now > g) {now = g;
Rep (i, 0, sz-1) b[i] = A[i];
} if (sz = = D[y]-1) return;
Loop (i, ft[x], NT) {if (D[u[i]]!= d[x] + 1) continue;
A[SZ] = MP (U[i], v[i]);
DFS (U[i], y, v[i], sz + 1, g + (int) (z!= v[i]));
} void BFs (int x, int y) {queue<int> p; p.push (x);
Rep (i, 0, 9999) d[i] = 0;
D[X] = 1;
while (!p.empty ()) {int q = P.front (); P.pop ();
Loop (i, Ft[q], NT) {if (D[u[i]]) continue; P.push (U[i]);
D[u[i]] = d[q] + 1;
} now = INF;
DFS (x, y, 0, 0, 0);
printf ("%d\n", D[y]-1);
int g = x;
B[d[y]-1] = MP (0, 0); Rep (i, 0, D[y]-2) {if (B[I].SS = = B[i + 1].ss) continue;
printf ("Take line#%d from%04d to%04d.\n", B[I].SS, X, B[I].FF);
x = B[i].ff;
int main () {Inone (n);
MS (FT,-1);
Rep (i, 1, n) {intwo (M, x);
Rep (J, 2, m) {Inone (y); U[sz] = y; V[SZ] = i; NT[SZ] = ft[x];
FT[X] = sz++; U[SZ] = x; V[SZ] = i; NT[SZ] = Ft[y];
Ft[y] = sz++;
x = y;
} inone (n);
Rep (i, 1, n) {intwo (x, y);
BFS (x, y);
return 0; }