欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1131 Subway Map(DFS)

程序员文章站 2022-03-13 22:44:32
...

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.

1131 Subway Map(DFS)

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.

1131 Subway Map(DFS)

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.

题目大意

n条地铁线路,给出每条线路所经过的站点编号,求出从起点s到终点e之间经过的站最少的路线,如果不唯一,输出换乘次数最少的路线。

思路

好久没见到这种让我脑壳发昏的题目了…哎,果然自己还是水平不够。
将整张地铁线路网当作一个图,用邻接矩阵储存(不区分线路)。而每条路的线路信息(属于那一条line)如何存储,我是看了别人的想法,用unordered_map(避免自动排序),10000 * path[i - 1] + path[i]作为key(这样可以保证每条路的key是唯一的),其所属line作为value。之后就比较简单了,用DFS即可。每搜索到一条路径,判断其经过的站点数cnt是否更小,如果相同的话,则判断其换乘次数transfer是否更小。如此,可得到最优路径。

AC代码

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map> 
using namespace std; 

const int maxn = 10001, INF = 99999;
vector<int> G[maxn];
unordered_map<int, int> mp;
bool vis[maxn];
int min_cnt, min_transfer; //最小站点数 最小换乘数 
vector<int> temp, path;
int s, e;

int transfer(vector<int> path){ //计算换乘次数
	int cnt = -1, preLine = 0;
	for(int i = 1; i < path.size(); i++){
		if(mp[path[i - 1] * 10000 + path[i]] != preLine)
			cnt++; 
		preLine = mp[path[i - 1] * 10000 + path[i]];
	}
	return cnt;
}

void DFS(int index, int cnt){
	if(index == e){
		if(cnt < min_cnt || cnt == min_cnt && transfer(temp) < min_transfer){
			//找到更优的路径
			min_cnt = cnt;
			min_transfer = transfer(temp);
			path = temp; 
		}
		return;
	}
	for(int i = 0; i < G[index].size(); i++){
		int v = G[index][i];
		if(vis[v] == false){
			vis[v] = true;
			temp.push_back(v);
			DFS(v, cnt + 1);
			vis[v] = false;
			temp.pop_back();
		}
	}
}

int main(){
	int n, m, k, u, v;
	cin>>n;
	for(int i = 1; i <= n; i++){
		cin>>m>>u;
		for(int j = 1; j < m; j++){
			cin>>v;
			G[u].push_back(v);
			G[v].push_back(u);
			mp[u * 10000 + v] = mp[v * 10000 + u] = i;
			u = v;
		}
	}
	cin>>k;
	for(int i = 0; i < k; i++){
		cin>>s>>e;
		temp.clear();
		temp.push_back(s);
		vis[s] = true;
		min_cnt = INF, min_transfer = INF;
		DFS(s, 0);
		vis[s] = false;
		printf("%d\n", min_cnt);
		int preLine = 0, ls = s;
		for(int j = 1; j < path.size(); j++){
			if(mp[path[j - 1] * 10000 + path[j]] != preLine){
				if(preLine != 0)
					printf("Take Line#%d from %04d to %04d.\n", preLine, ls, path[j - 1]);
				preLine = mp[path[j - 1] * 10000 + path[j]];
				ls = path[j - 1];
			}
		} 
		printf("Take Line#%d from %04d to %04d.\n", preLine, ls, e);
	}
	return 0;
}