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. Golang实现mysql读库映射成Map【Easy】

    这个类库灵感来源于.net的dbHelper类,因为其简单易用,现在go的driver必须使用对象映射,这让人火大不爽,不能实现灵活的Map,在Key经常变动的业务场景里面非常不爽,我还是喜欢直接写s ...

  2. Python和MySQL数据库交互PyMySQL

    Python数据库操作 对于关系型数据库的访问,Python社区已经指定了一个标准,称为Python Database API SepcificationV2.0.MySQL.Qracle等特定数据库 ...

  3. 14 Go's Declaration Syntax go语言声明语法

    Go's Declaration Syntax go语言声明语法 7 July 2010 Introduction Newcomers to Go wonder why the declaration ...

  4. cvpr densnet论文

  5. python进阶学习之高阶函数

    高阶函数就是把函数当做参数传递的一种函数, 例如: 执行结果: 1.map()函数 map()接收一个函数 f 和一个list, 并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 l ...

  6. 洛谷P2661信息传递

    传送门啦 一个人要想知道自己的生日,就意味着信息的传递是成环的,因为每轮信息只能传递一个人,传递的轮数就等于环的大小 环的大小就等于环中的两个点到第三个点的距离之和加一,我们就可以在使用并查集时,维护 ...

  7. SQL SERVER2008 镜像全攻略

    --在非域控环境中创建数据库镜像, 我们必须使用证书来创建数据库镜像. 大致的步骤包括: --在为数据库镜像配置的每个服务器实例上执行下列步骤: --在 master 数据库中,创建数据库主密钥. - ...

  8. Effective STL 笔记 -- Item 9: Choose carefully among erasing options

    假设有一个容器中存放着 int ,Container<int> c, 现在想从其中删除数值 1963,可以有如下方法: 1: c.erase(remove(c.begin(), c.end ...

  9. 饼图tooltip

    @{ ViewBag.Title = "pie"; } <h2>pie</h2> <div id="main" style=&qu ...

  10. 【hihoCoder】#1513 : 小Hi的烦恼

    题解 我会五维数点辣 只要用个bitset乱搞就好了 记录一下rk[i][j]表示第j科排名为i的是谁 用30000 * 5个大小为30000的bitset s[i][j]是一个bitset表示第j科 ...