PAT甲级1131. Subway Map
PAT甲级1131. Subway Map
题意:
在大城市,地铁系统对访客总是看起来很复杂。给你一些感觉,下图显示了北京地铁的地图。现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任务是找到他/她目的地的最快方式。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含正整数N(<= 100),地铁线数。然后N行跟随,第i(i = 1,...,N)行以格式描述第i条地铁线:
M S [1] S [2] ... S [M]
其中M(<= 100)是停止次数,S [i](i = 1,...)
M)是沿线的指标(指数是从0000到9999的4位数字)。确保车站以正确的顺序给出 - 即火车在S [i]和S [i + 1](i = 1,...,M-1)之间不间断地行驶。
注意:可以有循环,
循环(没有火车从S开始,S停止,不经过另一个站)。每个站间隔属于唯一的地铁线。虽然这些线路可能在某些车站(所谓的“转运站”)相互交叉,但是任何车站都不能超过5条线路。
在描述地铁后,
给出另一个正整数K(<= 10)。然后按K行,每个给出您的用户的查询:两个索引分别作为起始站和目的地。
下图显示了示例图。
注意:保证所有站点都可以访问,所有查询都包含合法站号。
输出规格:
对于每个查询,首先在一行中打印最少的停靠点数。那么你应该以友好的格式显示最佳路径,如下所示:
从X1到S2的线#X1。
从S2到S3取第X2行
。
......
其中Xi是线数,Si是站指数。注意事项:除了起点和终点站外,只能印制转运站。
如果最快的路径不是唯一的,输出最小数量的传输,这被保证是唯一的。
思路:
最短路。我一开始用Dijkstra + dfs除了最后一个超时,其他都是0ms。后来不用Dijkstra,只用dfs就过了。奇怪的是Dijkstra + dfs居然会慢一些 = =。。不过小数据Dijkstra + dfs 都是0ms 但是最后大数据。好像只用dfs貌似会快一些,但是小数据没有Dijkstra + dfs理想。。
这题问题就是加了一线路吧问题复杂化了。我用vector储存next站台。然后用的是pair<int,int>储存下一个站台的id和线路。一开始我吧线路的属性放到站台,这样的话transfer的线路属性就不知道是什么了。所以线路的属性在边上。就放到next里,用pair保存。
其实还有一种方法就是用5位数保存。前面四位数保存id,第一位保存线路。因为题目中数据范围已知,到时候只用求余等操作就可以了。
ac代码:
C++
// pat1131.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int starting, ending;
struct Station
{
int val;
vector<pair<int,int>> next;
};
Station st[10001];
int visit[10001];
int len[10001];
unordered_set<int> have;
void Dijkstra()
{
memset(visit, 0, sizeof(visit));
memset(len, -1, sizeof(len));
int now = -1;
unordered_set<int>::iterator it;
len[starting] = 0;
while (1)
{
now = -1;
for (it = have.begin(); it != have.end(); it++)
{
if (!visit[*it] && len[*it] != -1 && (now == -1 || len[*it] < len[now])) now = *it;
}
if (now == -1 || now == ending) break;
visit[now] = 1;
for (int i = 0; i < st[now].next.size(); i++)
{
if (!visit[st[now].next[i].first] && (len[st[now].next[i].first] == -1 || len[st[now].next[i].first] > len[now] + 1))
{
len[st[now].next[i].first] = len[now] + 1;
}
}
}
}
struct way
{
int s;
int e;
int line;
way(int x, int y, int z) : s(x) , e(y) , line(z) {}
};
vector<vector<way> > res;
vector<way> path;
int min_cnt;
void dfs(int now,int cur_cnt, int last,vector<way>& mytemp, int line)
{
//if (cur_cnt > len[ending]) return;
if (cur_cnt > min_cnt) return;
//if (now == ending && cur_cnt == len[ending])
if (now == ending && cur_cnt <= min_cnt)
{
mytemp.push_back(way(last, ending, line));
if (res.empty() || (mytemp.size() < res[0].size() && cur_cnt == min_cnt) || cur_cnt < min_cnt)
{
res.clear();
res.push_back(mytemp);
}
min_cnt = cur_cnt;
mytemp.pop_back();
return;
}
visit[now] = 1;
for (int i = 0; i < st[now].next.size(); i++)
{
if (!visit[st[now].next[i].first])
{
if (st[now].next[i].second != line && now != last)
{
mytemp.push_back(way(last, now, line));
dfs(st[now].next[i].first, cur_cnt + 1, now, mytemp, st[now].next[i].second);
mytemp.pop_back();
}
else
{
dfs(st[now].next[i].first, cur_cnt + 1, last, mytemp, st[now].next[i].second);
}
visit[st[now].next[i].first] = 0;
}
}
}
int main()
{
int n, m, id, last;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &m);
for(int j = 0; j < m; j++)
{
scanf("%d", &id);
have.insert(id);
st[id].val = id;
if (j > 0)
{
st[id].next.push_back(make_pair(last,i));
st[last].next.push_back(make_pair(id, i));
}
last = id;
}
}
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d %d", &starting, &ending);
//Dijkstra();
min_cnt = have.size();
memset(visit, 0, sizeof(visit));
vector<way> temp;
res.clear();
dfs(starting,0,starting,temp,1);
//printf("%d\n", len[ending]);
printf("%d\n", min_cnt);
for (int i = 0; i < res[0].size(); i++)
{
printf("Take Line#%d from %04d to %04d.\n", res[0][i].line,res[0][i].s,res[0][i].e);
}
}
return 0;
}
PAT甲级1131. Subway Map的更多相关文章
- PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
- PAT甲级1131 Subway Map【dfs】【输出方案】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...
- PAT甲级——A1131 Subway Map【30】
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 DFS解法 BFS回溯!
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 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
题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路. 思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边 ...
随机推荐
- Shell脚本系列教程二: 开始Shell编程
Shell脚本系列教程二: 开始Shell编程 2.1 如何写shell script? (1) 最常用的是使用vi或者mcedit来编写shell脚本, 但是你也可以使用任何你喜欢的编辑器; (2) ...
- day09作业
一.填空题 1.方法 2.堆内存 3.构造方法 4.this 5.this 6.static 7.使用类名进行访问 8.package import class 9.关键字 10.lang 二.选择题 ...
- ROS二进制日志包 ROS binary logger package
原文网址: 1 http://www.ros.org/news/2017/02/ros-binary-logger-package.html 2 https://github.com/CNR-ITIA ...
- javascript数组元素的添加、删除与插入以及参数数组的使用
1.数组元素的添加 push方法在数组的尾部添加元素: var colorArray=new Array(); colorArray.push('red','black','yellow'); //这 ...
- netty 并发访问测试配置
linux – 1.查看有关的选项 /sbin/sysctl -a|grep net.ipv4.tcp_tw net.ipv4.tcp_tw_reuse = 0 #表示开启重用.允许将 ...
- 百度Webuploader 大文件分片上传(.net接收)
前阵子要做个大文件上传的功能,找来找去发现Webuploader还不错,关于她的介绍我就不再赘述. 动手前,在园子里找到了一篇不错的分片上传的帖子,参考之后,踏出了第一步.此文记录我这次实践的点滴,仅 ...
- 【LOJ】#2056. 「TJOI / HEOI2016」序列
题解 这个我们处理出来每一位能变化到的最大值和最小值,包括自身 然后我们发现 \(f[i] = max(f[i],f[j] + 1) (mx[j] <= a[i] && a[j] ...
- 【AtCoder】ARC095 C-F题解
我居然每道题都能想出来 虽然不是每道题都能写对,debug了很久/facepalm C - Many Medians 排序后前N/2个数的中位数时排序后第N/2 + 1的数 其余的中位数都是排序后第N ...
- 自动化CI构建工具
hudson/maven jenkins,bamboo, hudson
- Hibernate or JPA Annotation中BLOB、CLOB注解写法
BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...