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 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.
最短路,求从起点到终点最短的地铁路线,要求经过的站最少,过站相同时线路换乘数应该最少。这道题麻烦的点就是统计最小的换乘数,以及最后输出换乘路线。
用优先队列实现,显然先按距离排序再按换乘数排序,统计所有可以到达a站的方案,在队首读取到的第一个a站的方案肯定是最短路径,其他方案排列在后面对结果不影响,因为后面的方案以及方案的下一站一定比队首方案远。
注意返回结果要在队首读取到终点站再返回,而不是在地铁站入队时返回,因为尽管当前读取的前一站是最短路径,但是不能保证下一站有没有换乘,而后面的方案没有换乘,假设最优方案在后面,提前返回会忽略最优方案。为了防止最优方案在前,其他方案在后,顶替掉最优方案,要记录每个站的最短距离和最少换乘,更优才更新入队。
记录路径的方法是标记每条边的所属线路,当前节点是从哪个线路的边到达的,以及在这条路线上的最早的换乘点,如果当前节点的线路和上个站的线路不一样,换乘点就是上个站,读取时根据换乘点递归输出。
#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
using namespace std;
typedef long long ll;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=20005;
const int sz=100005;
int n,m,k,cot,st,ed;
int fst[sz],nxt[sz],to[sz],num[sz],vis[sz],tr[sz],trn[sz],cnt[sz],ct[sz];
struct node{
int dis,tim,num;
bool operator <(const node x)const{
if(dis==x.dis)
return tim>x.tim;
else
return dis>x.dis;
}
};
void prt(int i){
if(i==st)
return;
prt(trn[i]);
printf("Take Line#%d from %04d to %04d.\n",tr[i],trn[i],i);
//printf("Take Line#%d from %04d to %04d.\n",x.tr[i],x.trn[i],ed);
}
void sch(int st,int ed){
if(st==ed){
printf("0\n");
return ;
}
node t;
t.dis=0;
t.num=st;
t.tim=0;
tr[st]=0;
trn[st]=0;
priority_queue<node> q;
q.push(t);
while(!q.empty()){
t=q.top();
q.pop();
if(t.num==ed){
printf("%d\n",t.dis);
prt(t.num);
return;
}
int i=t.num,f=t.num;
for(i=fst[i];i!=-1;i=nxt[i]){
int j=to[i];
if(!vis[i]){
vis[i]=1;
if(i>MAXN) vis[i-MAXN]=1;
else vis[i+MAXN]=1;
node nt;
nt.dis=t.dis+1;
if(ct[j]==-1||ct[j]>=nt.dis){
ct[j]=nt.dis;
}else{
continue;
}
nt.num=j;
nt.tim=t.tim;
int Trn,Tr;
Trn=trn[f];
Tr=tr[f];
if(num[i]!=tr[f]){
nt.tim++;
Tr=num[i];
Trn=f;
}
if(cnt[j]==-1||(ct[j]>0&&(ct[j]<nt.dis))||(ct[j]>0&&(ct[j]==nt.dis)&&(cnt[j]>nt.tim))){
cnt[j]=nt.tim;
tr[j]=Tr;
trn[j]=Trn;
//printf("##%d %d\n",j,cnt[j]);
}else{
continue;
}
//cout<<'!'<<nt.num<<' '<<nt.dis<<' '<<nt.tim<<' '<<num[i]<<endl;
q.push(nt);
//printf("**%d %d %d\n",nt.num,nt.dis,nt.tim);
}
}
}
}
int main()
{
//freopen("r.txt","r",stdin);
scanf("%d",&n);
cot=0;
int t,p;
memset(fst,-1,sizeof(fst));
for(int i=1;i<=n;i++){
scanf("%d",&k);
scanf("%d",&p);
for(int j=1;j<k;j++){
scanf("%d",&t);
nxt[++cot]=fst[p];fst[p]=cot;to[cot]=t;num[cot]=i;
nxt[cot+MAXN]=fst[t];fst[t]=cot+MAXN;to[cot+MAXN]=p;num[cot+MAXN]=i;
p=t;
}
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&st,&ed);
memset(vis,0,sizeof(vis));
memset(cnt,-1,sizeof(cnt));
memset(ct,-1,sizeof(ct));
sch(st,ed);
}
}
数据量很小,所以网上的题解用DFS暴力枚举也居然过了,但是效率太低