题目https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432

题意:

告诉你一个地铁线路图,站点都是用四位数来编号。

现在问你从某一起点到某一终点,经过站数最少的乘车方式是什么?要输出方案。

如果站数相同要输出换乘较少的。

思路:

首先肯定是搜索没有问题了。因为要输出方案,所以bfs不太方便存答案。很容易想到用dfs

比较麻烦的是输出方案的时候,线路相同的那些站都合并在同一个线路里了。那么我们就再维护一个乘车区间结构体。

如果当前走的边和当前答案最后一条边是同一个线路,就把答案中最后一个乘车区间的终点改为当前的点。

回溯的时候不仅要把vis数组恢复,还要把答案恢复成dfs之前的,所以用一个tmpans来记录一下本次dfs之前的答案。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; struct station{
int to;
int line;
station(){
}
station(int _to, int _line){
to = _to;
line = _line;
}
}; struct mapnode{
vector<station>list;
}; struct segment{
int st, ed;
int line;
segment(){}
segment(int _st, int _ed, int _line)
{
st = _st;
ed = _ed;
line = _line;
}
}; struct Ans{
int len;
vector<segment>anslist; bool operator < (const Ans b)const{
if(len == b.len)
return anslist.size() < b.anslist.size();
return len < b.len;
}
}; const int maxn = 1e5 + ;
mapnode sta[maxn];
int n, k; Ans ans, nowans;
bool vis[maxn];
void dfs(int st, int ed)
{
if(st == ed){
if(nowans < ans)ans = nowans;
//return;
} if(ans < nowans)return; for(int i = ; i < sta[st].list.size(); i++){
int to = sta[st].list[i].to;
if(!vis[to]){
Ans tmpans = nowans;
nowans.len++;
if(sta[st].list[i].line == nowans.anslist.back().line){
nowans.anslist.back().ed = to;
}
else{
nowans.anslist.push_back(segment(st, to,sta[st].list[i].line));
} vis[to] = true;
dfs(to, ed);
vis[to] = false;
nowans = tmpans;
}
}
} void init()
{
memset(vis, , sizeof(vis));
nowans.anslist.clear();
nowans.len = ;
nowans.anslist.push_back(segment(-, -, -));
ans.anslist.clear();
ans.len = inf; } int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
int m;
scanf("%d", &m);
int prev;
scanf("%d", &prev);
for(int j = ; j < m; j++){
int to;
scanf("%d", &to);
sta[prev].list.push_back(station(to, i));
sta[to].list.push_back(station(prev, i));
prev = to;
}
} scanf("%d", &k);
while(k--){
int st, ed;
scanf("%d%d", &st, &ed);
init();
dfs(st, ed);
printf("%d\n", ans.len);
for(int i = ; i < ans.anslist.size(); i++){
printf("Take Line#%d from %04d to %04d.\n", ans.anslist[i].line, ans.anslist[i].st, ans.anslist[i].ed);
}
} return ;
}

PAT甲级1131 Subway Map【dfs】【输出方案】的更多相关文章

  1. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

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

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

  3. PAT甲级——A1131 Subway Map【30】

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  4. 1131 Subway Map DFS解法 BFS回溯!

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  5. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

  6. PAT 1131 Subway Map

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  7. PAT 1131. Subway Map (30)

    最短路. 记录一下到某个点,最后是哪辆车乘到的最短距离.换乘次数以及从哪个位置推过来的,可以开$map$记录一下. #include<map> #include<set> #i ...

  8. 1131 Subway Map(30 分)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  9. 1131 Subway Map

    题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路. 思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边 ...

随机推荐

  1. CentOS 6下 Oracle11gR2 设置开机自启动

    [1] 更改/etc/oratab # This file is used by ORACLE utilities. It is created by root.sh # and updated by ...

  2. 安装 scws出现 autoconf 需要先安装

    安装在终端操作, curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-latest.tar.gz tar xzf autoconf-latest.t ...

  3. Gitbook 命令行工具

    1.Gitbook 简介 1.1 Gitbook GitBook 是一个基于 Node.js 开发的命令行工具,使用它可以很方便的管理电子书,GitBook 是目前最流行的开源书籍写作方案. 使用 G ...

  4. 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code- ...

  5. MySQL 的主从原理和复制过程简述

    一.MySQL 复制的基本过程如下:1. Slave 上面的IO线程连接上 Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容; 2. Master 接收到来自 Slav ...

  6. 物联网架构成长之路(5)-EMQ插件配置

    1. 前言 上一小结说了插件的创建,这一节主要怎么编写代码,以及具体流程之类的.2. 增加一句Hello World 修改 ./deps/emq_plugin_wunaozai/src/emq_plu ...

  7. epoll的由来

    reference https://www.zhihu.com/question/20122137 感谢 @静海听风 @蓝形参 数据流有两个重要的参与者: 1.往流中写入数据者 2.从流中读取数据者 ...

  8. iOS中如何创建一个滑出式导航面板(1)

    本文将介绍如何创建类似Facebook和Path iOS程序中的滑出式导航面板. 向右滑动 滑出式设计模式可以让开发者在程序中添加常用的导航功能,又不会浪费屏幕上宝贵的空间.用户可以在任意时间滑出导航 ...

  9. sqlmap tamter

    支持的数据库 编号 脚本名称 作用 实现方式 all 1 apostrophemask.py 用utf8代替引号 ("1 AND '1'='1") '1 AND %EF%BC%87 ...

  10. Java连接各种数据库写法

    # 示例配置参考,涵盖几乎所有的主流数据库 ############# Oracle数据库 ######################## #   数据库驱动名 driver=oracle.jdbc ...