PAT 甲级 1131 Subway Map (30 分) BFS+DFS

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.

这道题作为一个30分的题目,算法思路我觉得都很清晰,但是就是对数据的处理上,需要一点技巧能力。
1、一张地图上求最短距离,任意两点之间的权值相等,BFS好像就是为这种情况而生的,如果使用dijkstra,我真心不推荐,不同的算法解决不同的问题。dijkstra解决的是两个顶点之间权值不同的图的最短距离。
2、DFS的基本思想是遍历,可以找出路径来,可以使用上述BFS解出来的最短距离,作为剪枝的条件,可以大大减小计算量。
3、这道题非常关键的地方就是对换乘次数的计算,因为某一个车站可能同时属于多个线路,而每条边(相邻两个站点形成的边)是唯一的属于某条地铁线,利用这一点属性,可以标记边来解决。

总结一下

1、BFS解决地图最短距离问题,如果有时间可以去北大的openjudge上找搜索专练。注意,只能找出最短距离的值,不能找出路径,算法的形式类似于点的扩散
2、DFS一般是不可以直接使用的,直接使用的算法复杂度太高,使用的条件是可以有效的剪枝,这是DFS的关键点,一般都是和dijkstra算法,BFS放在一起,形成一道综合题目。用于求路径

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_map>
using namespace std;
int N; //北京地铁线路的数量
unordered_map<int,int>line; //标记线路,站点不属于唯一的线路,而线段唯一
vector<int> v[10005];  //每一个站点的信息,用向量存储相邻站点
int num=0;
bool visit[10005]={false}; //用来深度搜索
vector<int> ans;  //最终的路线
int trans; //换乘次数

void BFS(int src,int des)
{
    bool visit[10001]={false};
    pair<int,int> curr;
    queue<pair<int,int>> que;
    visit[src]=true;
    que.push(pair<int,int>(src,0));
    while(!que.empty())
    {
        curr=que.front();
        que.pop();
        if(curr.first==des)
        {
            num=curr.second;
            break;
        }
        for(int i=0;i<v[ curr.first ].size();i++)
            if(!visit[ v[curr.first][i] ])
            {
                visit[ v[curr.first][i] ]=true;
                que.push(pair<int,int>( v[curr.first][i],curr.second+1 ));
            }
    }
}
void DFS(int curr,int des,int t,vector<int> &route)
{
    if(curr==des&&t==num) //找出一条线路,满足num
    {
        bool S[105]={false};
        int line_num=0;
        for(int i=1;i<route.size();i++)
            if( !S[ line[ route[i]*10000+route[i-1] ] ] )
        {
            S[ line[ route[i]*10000+route[i-1] ] ] = true;
            line_num++;
        }
        if(line_num<trans) //保留换乘次数最少的路线
        {
            ans=route;
            trans=line_num;
        }
    }
    if(t>=num)
        return;
    for(int i=0;i<v[curr].size();i++)
        if(!visit[ v[curr][i] ])
        {
            visit[ v[curr][i] ] = true;
            route.push_back( v[curr][i] );
            DFS(v[curr][i],des,t+1,route);
            visit[ v[curr][i] ] = false;
            route.pop_back();
        }
}
int main()
{
    cin>>N;
    int k,a,pre;
    for(int i=1;i<=N;i++)
    {
        scanf("%d",&k);
        scanf("%d",&a);
        for(int j=1;j<k;j++)
        {
            pre=a;
            scanf("%d",&a);
            v[pre].push_back(a);
            v[a].push_back(pre);
            line[pre*10000+a]=i; //两个站点确定一个路线
            line[a*10000+pre]=i;
        }
    }
    int src,des;
    cin>>k;
    while(k--)
    {
        scanf("%d%d",&src,&des);
        BFS(src,des);  //使用BFS广度优先搜索求出最短路径,然后再使用DFS搜索,求出路径

        vector<int> t;
        ans.clear();
        trans=0x7fffffff; //初始化换乘次数
        fill(visit,visit+10001,false);
        visit[src]=true;
        t.push_back(src);
        DFS(src,des,0,t);
    //下面开始对结果输出
        cout<<num<<endl;
        int pre=0,stop=0,i=0;
        for(i=1;i<ans.size();i++)
        {
            if( line[ ans[i]*10000+ans[i-1] ] != pre )
            {
                if(pre!=0)
                    printf("Take Line#%d from %04d to %04d.\n",pre,stop,ans[i-1]);
                stop=ans[i-1];
                pre=line[ ans[i]*10000+ans[i-1] ];
            }
        }
        printf("Take Line#%d from %04d to %04d.\n",pre,stop,ans[i-1]);
    }
    return 0;
}
发布了174 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41173604/article/details/100529589