PAT 1131. Subway Map (30)-Grade A

Source: Internet
Author: User
Tags printf

1131. Subway Map ($)
The big cities, the subway systems always look so complex to the visitors. To give your some sense, the following figure shows the map of Beijing Subway. Now is supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest-to his/her.

Input Specification:

Each input file contains the 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 (<=) is the number of stops, and S[i] ' S (i = 1, ... M) is the indices of the stations (the indices is 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations was 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 has loops, but not self-loop (no train starts from s and stops at s without passing through a Nother station). Interval belongs to a unique subway line. Although the lines may cross each of the some stations (so called "transfer stations"), no station can is the Conjunctio N of more than 5 lines.

After the description of the subway, another positive an integer K (<=) is given. Then K lines follow, each gives a query from your user:the both indices as the starting station and the destination, Respe ctively.

The following figure shows the sample map.
Note:it is guaranteed that all the stations is reachable, and all the queries consist of the legal station numbers.

Output Specification:

for each query, first print with a line the minimum number of stops. Then you is 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 is the line numbers and Si ' s is the station indices. Note:besides the starting and ending stations, only the transfer stations shall is printed.

If The quickest path is isn't unique, output the one with the minimum number of transfers, which is guaranteed to be uni Que.

Sample Input:
4
7 1001 3212 1003 1204 1005 1306 7797
Strong>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.

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.

The main idea of the topic: To find a route, so that any given starting point and end point, you can find the route of the least halfway through the stop station; if there are as many stops, take the route with the least number of transfers-
Analysis: "Very simple, don't run ^" _^ "Once Dfs can ~dfs the process to maintain two variables: mincnt-the fewest stops in the middle of the stop; mintransfer-minimum number of transfers required ~
0. You can calculate the number of transfers for a line: in line[10000][10000] The array in which to save each of the two adjacent stations in the middle of the line is the line ~ from beginning to end to traverse the final saved path, Preline for the previous small segment of the line number, if the current node and the previous node composed of the route number and preline different, indicating that there is a transfer, will cnt+ 1, the final traversal of the accumulated CNT is the number of transfers ~
1. You can calculate the number of stops in a line: At DFS, there is a variable cnt, which indicates that the current route is the number of stations required to multiply, and each time Dfs is cnt+ 1 means that traversing a layer down ~cnt is the current number of stopovers ~
2. You can output the result as follows: The idea of calculating the number of lines Whenever the preline and the current line value are different, output a sentence ~ Save Pretransfer represents the last transfer station, and finally do not forget the output Pretransfer and the last station between the road even if the last station is not a transfer station ~

#include <cstdio> #include <vector> using namespace std;
Vector<vector<int>> V (10000);
int line[10000][10000], visit[10000];
vector<int> path, TempPath;
    int transfercnt (vector<int> a) {int cnt =-1, preline = 0;
        for (int i = 1; i < a.size (); i++) {if (Line[a[i-1]][a[i]]! = preline) cnt++;
    Preline = Line[a[i-1]][a[i]];
} return CNT; } void Dfs (int node, int end, int cnt, int &mincnt, int &mintransfer) {if (node = = End && (CNT < m incnt | | (cnt = = mincnt && transfercnt (TempPath) < Mintransfer)))
        {mincnt = cnt;
        Mintransfer = transfercnt (TempPath);
    Path = TempPath;
    } if (node = = end) return;
            for (int i = 0; i < v[node].size (); i++) {if (visit[v[node][i] = = 0) {Visit[v[node][i]] = 1;
            Temppath.push_back (V[node][i]);
            DFS (V[node][i], end, CNT + 1, mincnt, Mintransfer); Visit[v[node][i]] = 0;
            Temppath.pop_back ();
    }}} int main () {int n, m, K, Pre, temp, A, B;
    scanf ("%d", &n);
        for (int i = 0; i < n; i++) {scanf ("%d%d", &m, &pre);
            for (int j = 1; j < M; J + +) {scanf ("%d", &temp);
            V[pre].push_back (temp);
            V[temp].push_back (pre);
            Line[pre][temp] = Line[temp][pre] = i + 1;
        Pre = temp;
    }} scanf ("%d", &k);
        for (int i = 0; i < K; i++) {scanf ("%d%d", &a, &b);
        int mincnt = 99999, Mintransfer = 99999;
        Temppath.clear ();
        Temppath.push_back (a);
        Visit[a] = 1;
        DFS (A, b, 0, mincnt, mintransfer);
        Visit[a] = 0;
        printf ("%d\n", mincnt);
        int preline = 0, pretransfer = A; for (int j = 1; J < Path.size (); j + +) {if (line[path[j-1]][path[j]]! = preline) {if (PreL Ine! = 0) printf ("Take line#%d from%04d to%04d.\n", PREline, Pretransfer, path[j-1]);
                Preline = Line[path[j-1]][path[j]];
            Pretransfer = Path[j-1];
    }} printf ("Take line#%d from%04d to%04d.\n", Preline, Pretransfer, b);
} return 0; }

Meow ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.