题目描述
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.
翻译:每个输入文件包含一组测试数据。对于每组测试数据,第一行包括一个正整数N (≤ 100), 表示地铁线路数。接下来N行,第i行按照以下格式给出铁路线:
M S[1] S[2] … S[M]
M (≤ 100)表示站的个数,S[i] (i=1,⋯,M) 表示每一站的编号(编号为4位从0000到9999的数字)。数据保证车站按照正确顺序给出,即列车在S[i]到S[i+1] (i=1,⋯,M−1)的途中没有多余站。
注意:可能有循环,但不能有自循环(没有火车就从S发车停在S中间不经过另一站)。每个车站间隔属于唯一的地铁线路。虽然在某些站点(所谓的“中转站”),线路可能会交叉,但是任何站点都不能连接超过5条线路。
在地铁的描述后面,给出另一个正整数K (≤10)。接下来的K行,每一行都给出一个来自用户的查询:两个编号,分别作为起点和终点。
下图显示了示例图。
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.
翻译:对于每个查询,首先输出一行最少的站数。接着你需要用一种友好的方式来展示最佳路径像下面这样:
Take Line#X1 from S1 to S2.
Take Line#X2 from S2 to S3.
…
Xi表示铁路线路,Si表示车站的编号。注意:在起始站和终点站之间,只有转乘站会被输出。如果最快的路径不是唯一的,则输出转乘次数最少的路径,数据保证是最优线路是唯一的。
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 3001
Sample 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.
解题思路
这道题我用BFS算法写了半天,最后得出结果:BFS不是很适合做这种要整条路径结束后再判断大小的题目。BFS适合只需要判断当前点即可取到最优解的题目。这道题的问题在于如果到某一点路径长度相同且转乘次数相同时,BFS无法确定选择一条,因为这需要根据后面的路径的线路来判断。所以最后选择了DFS深度优先搜索,参考了柳婼柳神的方法,通过map来保存两站线路。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
int N,K,Q;
int st,en;
map<int,int>mp;
vector<int>node[10010];
vector<int>tmpPath,ansPath;
int ansCount,ansTransfer;
int transfer(vector<int>a){
int tmpCount=0,tmpLine=0;
for(int i=1;i<a.size();i++){
if(mp[a[i-1]*10000+a[i]]!=tmpLine){
tmpCount++;
tmpLine=mp[a[i-1]*10000+a[i]];
}
}
return tmpCount;
}
int v[10010];
void dfs(int root,int ccount){
if(ccount>ansCount)return;
if(root==en&&(ccount<ansCount||(ccount==ansCount&&transfer(tmpPath)<ansTransfer))){
ansTransfer=transfer(tmpPath);
ansCount=ccount;
ansPath=tmpPath;
}
if(root==en)return ;
for(int i=0;i<node[root].size();i++){
if(!v[node[root][i]]){
v[node[root][i]]=1;
tmpPath.push_back(node[root][i]);
dfs(node[root][i],ccount+1);
v[node[root][i]]=0;
tmpPath.pop_back();
}
}
}
void Print(){
int tmpCount=0,tmpLine=0,l=st;
printf("%d\n",ansCount);
for(int i=1;i<ansPath.size();i++){
if(mp[ansPath[i-1]*10000+ansPath[i]]!=tmpLine){
if(tmpLine)printf("Take Line#%d from %04d to %04d.\n",tmpLine,l,ansPath[i-1]);
tmpLine=mp[ansPath[i-1]*10000+ansPath[i]];
l=ansPath[i-1];
tmpCount=1;
}
else {
tmpCount++;
}
}
printf("Take Line#%d from %04d to %04d.\n",tmpLine,l,en);
}
int main(){
scanf("%d",&N);
for(int i=1;i<=N;i++){
scanf("%d",&K);
int tmp=-1,tmpstation;
for(int j=0;j<K;j++){
scanf("%d",&tmpstation);
if(tmp>=0){
node[tmp].push_back(tmpstation);
node[tmpstation].push_back(tmp);
mp[tmp*10000+tmpstation]=i;
mp[tmpstation*10000+tmp]=i;
}
tmp=tmpstation;
}
}
scanf("%d",&Q);
for(int i=0;i<Q;i++){
scanf("%d%d",&st,&en);
memset(v,0,sizeof(v));
ansPath.clear();
tmpPath.clear();
ansCount=INF,ansTransfer=INF;
v[st]=1;
tmpPath.push_back(st);
dfs(st,0);
Print();
}
return 0;
}