In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you 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 destination.

Input Specification:
Each input file contains one test case. For each case, the first line 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 (<= 100) is the number of stops, and S[i]'s (i = 1, ... M) are the indices of the stations (the indices are 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations are given in the correct order -- that 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 another station). Each station interval belongs to a unique subway line. Although the lines may cross each other at some stations (so called "transfer stations"), no station can be the conjunction of more than 5 lines.
After the description of the subway, another positive integer K (<= 10) is given. Then K lines follow, each gives a query from your user: the two indices as the starting station and the destination, respectively.
The following figure shows the sample map.

Note: It is guaranteed that all the stations are reachable, and all the queries consist of legal station numbers.
Output Specification:
For each query, first print in a line 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's are the station indices. Note: Besides the starting and ending stations, only the transfer stations shall be printed.
If the quickest path is not unique, output the one with the 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 2005 2004 2003 2302 2001 13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011 4 6666 8432 4011 1306 3 3011 3013 6666 2001 2004 3001Sample Output:
2 Take Line#3 from 3011 to 3013. 10 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 2004 to 1204. Take Line#1 from 1204 to 1306. Take Line#3 from 1306 to 3001.
题目大意:
给你N条地铁线(1-N),k个询问(s,t),求从s到t距离最短,距离相同则换乘最少的路线(保证唯一)。
解题思路:
1、建图:每条地铁线相邻边保存信息(到达地点和地铁线号);
2、求最短距离:BFS搜一遍把由s出发的最短距离图求出来,用作后面深搜的剪枝(省时间)
3、求最短换乘路线:DFS搜一遍,不断更新换成的次数,知道求出来一个最少换乘的路线(求换成次数是在DFS时候把路径上经过的边保存,如果路线上相邻地铁线不一样就说明换乘了)。
#include <map>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 1e4+ 10;
const int MAXM = 1e4 + 10;
const int INF = 0x7f7f7f7f;
const int MOD = 1000000007;
int n, m, q;
struct NODE{
int to, line;
}node;
vector<NODE>mp[MAXN], temp, ans;
bool vis[MAXN];
int dis[MAXN];
int min_transfer, min_stop;
int Bfs(int s, int t){///先把s->t最短距离求出来,做后面的BFS剪枝,节省时间
queue<int>que;
que.push(s);
memset(vis, false, sizeof vis);
vis[s]=true;
dis[s]=0;
while(!que.empty()){
int fro=que.front();
que.pop();
for(int i=0; i<mp[fro].size(); i++){
if(!vis[mp[fro][i].to]){
que.push(mp[fro][i].to);
vis[mp[fro][i].to]=true;
dis[mp[fro][i].to]=dis[fro]+1;
}
}
}
return dis[t];
}
int Get_transfer(){///获取这条路的换乘次数
int cnt=0;
for(int i=1; i<temp.size(); i++){
if(temp[i].line!=temp[i-1].line) cnt++;
}
return cnt;
}
void Dfs(int s, int t, int stop){
if(s==t){///到达终点
int tra=Get_transfer();
if(tra<min_transfer) ans=temp, min_transfer=tra;
}else if(stop==min_stop) return;///剪枝1:没有到终点,但是距离多了
for(int i=0; i<mp[s].size(); i++){
int nxt=mp[s][i].to;
if(!vis[nxt] && dis[nxt]==dis[s]+1){///剪枝2:在最短路上
vis[nxt]=true, temp.push_back(mp[s][i]);
Dfs(nxt, t, stop+1);
vis[nxt]=false, temp.pop_back();
}
}
}
int main(){
while(~scanf("%d", &n)){///建图+Dfs
for(int i=0; i<MAXN; i++) mp[i].clear();
int s, t;
for(int i=1; i<=n; i++){///no.subway
node.line=i;
scanf("%d", &m);
if(m) scanf("%d", &s);
for(int j=1; j<m; j++){///stops
scanf("%d", &t);
node.to=t;mp[s].push_back(node);
node.to=s;mp[t].push_back(node);
s=t;
}
}
scanf("%d", &q);
while(q--){
scanf("%d%d", &s, &t);
min_stop = Bfs(s, t);///求最短距离
temp.clear();
min_transfer=INF;
memset(vis, false, sizeof vis);
Dfs(s, t, 0);
vector<NODE>pt;
bool flag=false;
for(int i=1; i<ans.size(); i++){
if(ans[i].line!=ans[i-1].line){
pt.push_back(ans[i-1]);
if(i==ans.size()-1) flag=true, pt.push_back(ans[i]);///最后一站是换乘站
}
}
if(!flag) pt.push_back(ans[ans.size()-1]);///最后一站不是换乘站要把最后一站加上
printf("%d\n", min_stop);
if(pt.size())printf("Take Line#%d from %04d to %04d.\n",pt[0].line, s, pt[0].to);
for(int i=1; i<pt.size(); i++) printf("Take Line#%d from %04d to %04d.\n",pt[i].line, pt[i-1].to, pt[i].to);
}
}
return 0;
}