1131 Subway Map (30 分)

1131 Subway Map (30 分)

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

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.

地铁模拟路线,dfs就能直接走出来。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n, m, p, x = 0, y = 0;
 4 int vis[10000][10000];
 5 vector<int> v[10000], vt, path;
 6 int val[10000];
 7 int minlen, minpath;
 8 int getpath(vector<int> a){
 9     int cnt = 0;
10     int present = 0;
11     for(int i = 1; i < a.size(); i++){
12         if(vis[a[i-1]][a[i]] != present)
13             cnt++;
14         present = vis[a[i-1]][a[i]];
15     }
16     return cnt;
17 }
18 void dfs(int st, int k){
19     if((st == y) && ((k < minlen)||(k == minlen && minpath > getpath(path)))){
20         minlen = k;
21         minpath = getpath(path);
22         vt = path;
23         return ;
24     }
25     for(int i = 0; i < v[st].size(); i++){
26         if(val[v[st][i]]) continue;
27         val[v[st][i]] = 1;
28         path.push_back(v[st][i]);
29         dfs(v[st][i], k+1);
30         path.pop_back();
31         val[v[st][i]] = 0;
32     }
33 }
34 int main(){
35     scanf("%d", &n);
36     for(int i = 1; i <= n; i++){
37         scanf("%d", &p);
38         for(int j = 0; j < p; j++){
39             y = x;
40             scanf("%d", &x);
41             if(j){
42                 vis[x][y] = vis[y][x] = i;
43                 v[x].push_back(y);
44                 v[y].push_back(x);
45             }
46         }
47     }
48     scanf("%d", &m);
49     while(m--){
50         scanf("%d%d", &x,&y);
51         minlen = 999999, minpath = 999999;
52         path.clear();
53         path.push_back(x);
54         val[x] = 1;
55         dfs(x, 0);
56         val[x] = 0;
57         printf("%d\n", minlen);
58         int line = 0, point = x;
59         for(int i = 1; i < vt.size(); i++){
60             if(vis[vt[i-1]][vt[i]] != line){
61                 if(line != 0) printf("Take Line#%d from %04d to %04d.\n", line, point, vt[i-1]);
62                 point = vt[i-1];
63                 line = vis[vt[i-1]][vt[i]];
64             }
65         }
66         printf("Take Line#%d from %04d to %04d.\n", line, point, y);
67     }
68     return 0;
69 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11318081.html

时间: 2024-08-02 02:37:21

1131 Subway Map (30 分)的相关文章

PAT甲级——1131 Subway Map (30 分)

可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 分) 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

1131 Subway Map(30 分)

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

PAT-1111 Online Map (30分) 最短路+dfs

明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好 1.没有读清题目要求,或者说没有读完题目,明天一定要注意 2.vis初始化的时候从1初始化到n,应该从0开始,以后初始化就从0到n即可 题目大意:给一张地图,两个结点中既有距离也有时间,有的单行有的双向,要求根据地图推荐两条路线:一条是最快到达路线,一条是最短距离的路线. 第一行给出两个整数N和M,表示地图中地点的个数和路径的条数.接下来的M行每一行给出:道路结点编号V1 道路结点编号V2 是否单行线 道

1131 Subway Map

link #include <iostream> #include <cstring> #include <vector> #include <unordered_map> #include <climits> # define LL long long using namespace std; vector<vector<int>> adj(10000); unordered_map<int,unordered_m

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of

pta08-图7 公路村村通 (30分)

08-图7 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N):随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本.为简单起见,城镇从1到N编号. 输出格式: 输出村村通需要的最低成本.如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路. 输入样例: 6

pta5-9 Huffman Codes (30分)

5-9 Huffman Codes   (30分) In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem o

PTA 1004 Counting Leaves (30)(30 分)(建树dfs或者bfs,未AC 段错误)

1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 10

1127 ZigZagging on a Tree (30 分)树的层次遍历

1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to pr