PAT甲级1131 Subway Map【dfs】【输出方案】
题目: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】【输出方案】的更多相关文章
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
- PAT甲级——A1131 Subway Map【30】
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 1131 Subway Map DFS解法 BFS回溯!
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
- PAT 1131 Subway Map
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- PAT 1131. Subway Map (30)
最短路. 记录一下到某个点,最后是哪辆车乘到的最短距离.换乘次数以及从哪个位置推过来的,可以开$map$记录一下. #include<map> #include<set> #i ...
- 1131 Subway Map(30 分)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 1131 Subway Map
题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路. 思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边 ...
随机推荐
- Eureka服务注册中心相关错误com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
启动项目报错如下 原因: 在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以会出现 com.sun.jersey.api.client.ClientHandlerExce ...
- 002_ASP.NET 换主题
网盘下载地址 http://pan.baidu.com/s/1c1VzIla 在线观看地址 http://www.bamn.cn/course/lesson/2 课程介绍 ASP.NET 实现更换主题 ...
- C#获取网页的HTML码、下载网站图片 get post
/// <summary> /// 获取网页的HTML码 /// </summary> /// <param name="url">链接地址&l ...
- 保持APP后台NSTimer运行
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; self.timer = [NSTi ...
- 2D空间中求线段与圆的交点
出处: https://answers.unity.com/questions/366802/get-intersection-of-a-line-and-a-circle.html 测试脚本(返回值 ...
- Atitit 视图参数解决方案 oracle版和mysql版本 attilax总结.docx
Atitit 视图参数解决方案 oracle版和mysql版本 attilax总结.docx 1.1. Package机制1 1.2. 全局变量机制1 1.3. 临时表模式,oracle mysql都 ...
- Atitit Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0
Atitit Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0 Uncaught (in ...
- 简单的topK问题
/************************************************************************/ /* 求一组数据中的top(K)问题,这是一个经典 ...
- git笔记-9-29
//将工作区的a.txt文件更新到最后一次提交到本地仓库的状态,如果需要将文件回滚到某个特定的版本,将HEAD改成那个commit的id即可 git checkout HEAD a.txt //如果将 ...
- SSH免登录及原理
1.免登陆实现 1)在本机生成公钥/私钥对 ssh-keygen 执行成功后,在.ssh文件夹下,会多出两个文件 id_rsa和id_rsa.pub 2)将公钥写入远端服务器.ssh文件夹下的auth ...