zoukankan      html  css  js  c++  java
  • 1131 Subway Map

    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.

    subwaymap.jpg

    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 (,) 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 (,) 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] (,) 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.

    samplemap.jpg

    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.

    题意:

      用一张无权无向图来表示一个城市的地铁,求出从起始地点到达目标地点的最少换乘的方案。

    思路:

      首先需要将两站点之间是属于几号线这个问题解决了,我们用unordered_map<int, int> lines来表示,first = station1*100000 + station2,second = 地铁线路。然后用广搜来找从start到destination的最短路径,这样找到的最短路径可能有多条,我们需要再用深搜来找到换乘最少的最短路径。无权连通图用邻接表来表示。dis[i]表示从起点到达结点i的最短路径。将dis初始化为INT_MAX,然后通过判断dis[i] != INT_MAX来判断结点i是不是已经遍历过了。二维vector数组past[][]用来存储在寻找最短路的过程中当前结点的先驱结点。

      这样所有的最短路径都存在了past数组中了,然后我们需要通过DFS来寻找最短路径中换乘次数最少的那条路线。DFS目标站点dest开始搜索,通过past[dest]来寻找当前节点的先驱结点,将所有的先驱结点都存在path数组中,当找到start之后,根据path数组中的记录找到相邻路径点之间是否换乘。这里用到了三个节点我们用i-1, i, i+1来表示这三个结点,判断是否换乘只需要判断lines[path[i-1]*10000+path[i]] == lines[path[i]*10000+path[i+1]]是否相等。如果换乘的话则将其存入临时的tempTrans数组中记录。因为有多条最短路径,所以在查找每条最短路径的时候,Trans都要找长度最短的tempTrans来进行更新。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 unordered_map<int, int> lines;
     6 vector<pair<int, int> > trans, tempTrans;
     7 vector<int> grap[10005], past[10005], path, temp, dis(10005);
     8 int n, m, k, start, dest;
     9 
    10 void BFS() {
    11     queue<int> que;
    12     // 初始化
    13     for (int i = 0; i < 10005; ++i) {
    14         past[i].clear();
    15         dis[i] = INT_MAX;
    16     }
    17     que.push(start);
    18     dis[start] = 0;
    19     while (!que.empty()) {
    20         int q = que.front();
    21         que.pop();
    22         if (dis[q] > dis[dest]) continue;
    23         for (int g : grap[q]) {
    24             if (dis[g] >= dis[q] + 1) {
    25                 if (dis[g] == INT_MAX) que.push(g);
    26                 dis[g] = dis[q] + 1;
    27                 if (dis[g] > dis[q] + 1) past[g].clear();
    28                 past[g].push_back(q);
    29             }
    30         }
    31     }
    32 }
    33 
    34 void DFS(int d) {
    35     path.push_back(d);
    36     if (d == start) {
    37         tempTrans.clear();
    38         tempTrans.push_back({start, -1});
    39         for (int i = path.size() - 2; i > 0; --i) {
    40             if (lines[path[i + 1] * 10000 + path[i]] !=
    41                 lines[path[i] * 10000 + path[i - 1]]) {
    42                 tempTrans.push_back(
    43                     {path[i], lines[path[i] * 10000 + path[i + 1]]});
    44             }
    45         }
    46         tempTrans.push_back({dest, lines[path[1] * 10000 + dest]});
    47         if (trans.size() > tempTrans.size() || trans.size() == 0) {
    48             trans = tempTrans;
    49         }
    50     }
    51     for (int p : past[d]) DFS(p);
    52     path.pop_back();
    53 }
    54 
    55 int main() {
    56     cin >> n;
    57     for (int i = 1; i <= n; ++i) {
    58         cin >> m;
    59         temp = vector<int>(m);
    60         for (int j = 0; j < m; ++j) cin >> temp[j];
    61         for (int j = 1; j < m; ++j) {
    62             lines[temp[j] * 10000 + temp[j - 1]] = i;
    63             lines[temp[j - 1] * 10000 + temp[j]] = i;
    64             grap[temp[j]].push_back(temp[j - 1]);
    65             grap[temp[j - 1]].push_back(temp[j]);
    66         }
    67     }
    68     cin >> k;
    69     for (int i = 0; i < k; ++i) {
    70         cin >> start >> dest;
    71         
    72         BFS();
    73         path.clear();
    74         trans.clear();
    75         DFS(dest);
    76         cout << dis[dest] << endl;
    77         for (int i = 0; i < trans.size() - 1; ++i) {
    78             printf("Take Line#%d from %04d to %04d.
    ", trans[i + 1].second,
    79                    trans[i].first, trans[i + 1].first);
    80         }
    81     }
    82 
    83     return 0;
    84 }

      看到这道题的第一感觉就是这道题不好做,写的时候发现真的很难,于是,果断放弃,研究大佬的代码去了。(参考:https://blog.csdn.net/richenyunqi/article/details/88394922

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    How To Configure Server Side Transparent Application Failover [ID 460982.1]
    10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
    AIX 系统介绍
    10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
    Parameter DIRECT: Conventional Path Export Versus Direct Path Export [ID 155477.1]
    Linux下 数据文件 效验问题
    open_links_per_instance 和 open_links 参数说明
    Oracle 外部表
    Export/Import DataPump Parameter ACCESS_METHOD How to Enforce a Method of Loading and Unloading Data ? [ID 552424.1]
    Linux下 数据文件 效验问题
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12745403.html
Copyright © 2011-2022 走看看