题目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. python dash 初探 --- k 线国内版

    python dash 的应用首页,是用一个 k 线图来做 damo 的,奈何数据源用的 Google,上不去.当然,可以换 yahoo,但是毕竟国内的还是更亲切些. 官方的 demo 用的 pand ...

  2. TerminateProcess的使用问题

    最好时外部进程来结束目标进程,类似于任务管理器的结束目标进程方式.如果是自身进程想结束自身,可能不同版本的windows行为不一致,有一些能自身强制退出,有一些强制退出不了. 本来MSDN上就说了这个 ...

  3. http头文件User-Agent详解【转载】

    原文地址:http://blog.csdn.net/andybbc/article/details/50587359 http头文件User-Agent详解 什么是User-Agent User-Ag ...

  4. Atitit 计算word ppt文档的页数

    Atitit 计算word ppt文档的页数 http://localhost:8888/ http://git.oschina.net/attilax/ati_wordutil private vo ...

  5. 【emWin】例程二十二:窗口对象——Framewin

    简介: 框架窗口为您的应用提供一个PC 应用程序的窗口外观.这些窗口由周围框架.标题栏和用户区组成. 触摸校准(上电可选择是否进入校准界面) 截图 实验指导书及代码包下载: 链接:http://pan ...

  6. PaaS 应用引擎

    这里主要是梳理一下应用引擎(XXXX App Engine),它一般被归类到PaaS领域.应用引擎即提供了各种编程语言开发的应用所需的一整套运行环境:它开箱即用,你只需部署应用的代码即可,无需前期的环 ...

  7. OllyScripts 0.92帮助文档

    -------------------------------Olly脚本插件v0.92 制作: SHaG文档汉化:ZMWorm[CCG][TT]E-Mail:TranslationTeam[at]1 ...

  8. android 监听动画对象后不能播放动画

    采用监听  AnimationListener 发现不能播放动画了. 解决办法: 将动画的启动方式:animation.startnow去掉,改为如下即可 view.startAnimation(an ...

  9. recyclerView插入(add)和删除(remove)item后,item错乱,重复,覆盖在原recyclerView上

    项目用到,实现一个recyclerView列表的item翻转动效,翻转的同时会将指定item置顶. (比如交换AB位置,A在0位置,指定的item B 在 i 位置) 原始使用的是插入B到0位置,然后 ...

  10. windows下更换pip源

    (1)在windows文件管理器中,输入 %APPDATA% (2)会定位到一个新的目录下,在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini文件 (3)在新建的pip.ini文 ...