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的更多相关文章

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

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

  2. PAT甲级1131 Subway Map【dfs】【输出方案】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...

  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. PAT甲级1111. Online Map

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

  5. PAT 1131 Subway Map

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

  6. PAT 1131. Subway Map (30)

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

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

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

  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. MySQL分布式集群之MyCAT(一)简介【转】

    隔了好久,才想起来更新博客,最近倒腾的数据库从Oracle换成了MySQL,研究了一段时间,感觉社区版的MySQL在各个方面都逊色于Oracle,Oracle真的好方便!好了,不废话,这次准备记录一些 ...

  2. unity3d 材质概述 ---- shader

    学习笔记:      材质概述:  物体呈现在我们前面除了形体外,还包括“固有颜色”和“质地”(质感与光学性质).固有颜色让物体的表面看起来是什么颜色,而质感决定了该物质是使用什么材质的.在三维建模软 ...

  3. Scala中“=>”用法及含义

    => has several meanings in Scala, all related to its mathematical meaning as implication. 1. In a ...

  4. Golang新起航!(编译安装go)

    别废话,直接上~ linux下安装GO1.8 1.下载go的版本 国内地址源:https://dl.gocn.io/ 在这里选择源码的方式安装,在安装go的时候是需要gcc的,所以你的linux系统需 ...

  5. 如何使用 JMeter 调用你的 Restful Web Service?进行简单的压力测试和自动化测试

    表述性状态传输(REST)作为对基于 SOAP 和 Web 服务描述语言(WSDL)的 Web 服务的简单替代,在 Web 开发上得到了广泛的接受.能够充分证明这点的是主流 Web 2.0 服务提供商 ...

  6. CI框架整合UEditor编辑器上传功能

    最近项目中要使用到富文本编辑器,选用了功能强大的UEditor,接下来就来讲讲UEditor编辑器的上传功能整合. 本文UEditor版本:ueditor1_4_3_utf8_php版本 第一步:部署 ...

  7. **CI中使用IN查询(where_in)

    注意别漏了$this->db->get(); /** * 匹配用户手机号,返回匹配的用户列表 * @param $column_str 'user_id, user_name, user_ ...

  8. Visual Studio 2013百度云下载地址

    Visual Studio 2013百度云下载地址: 链接: https://pan.baidu.com/s/1JkVYLnFo2TWSu4dkDdZP3Q  提取码: 关注公众号[获取   winf ...

  9. 解决Python爬虫使用requests包控制台输出乱码问题

    输出爬取的信息为乱码! 解决办法 爬取下来的编码是ISO-8859-1格式,需要转化为utf-8格式,加一句response.encoding = "utf8"

  10. 安装caffe框架所需文件

    安装caffe框架所需文件: 1.微软提供的快速卷积神经网络框架caffe-master安装包或者windows提供的caffe-windows安装包. 链接:http://pan.baidu.com ...