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++

  1. // pat1131.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<string>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<vector>
  10. #include<cstring>
  11. #include<stdio.h>
  12. #include<map>
  13. #include<cmath>
  14. #include<unordered_map>
  15. #include<unordered_set>
  16. using namespace std;
  17. int starting, ending;
  18. struct Station
  19. {
  20. int val;
  21. vector<pair<int,int>> next;
  22. };
  23. Station st[10001];
  24. int visit[10001];
  25. int len[10001];
  26. unordered_set<int> have;
  27. void Dijkstra()
  28. {
  29. memset(visit, 0, sizeof(visit));
  30. memset(len, -1, sizeof(len));
  31. int now = -1;
  32. unordered_set<int>::iterator it;
  33. len[starting] = 0;
  34. while (1)
  35. {
  36. now = -1;
  37. for (it = have.begin(); it != have.end(); it++)
  38. {
  39. if (!visit[*it] && len[*it] != -1 && (now == -1 || len[*it] < len[now])) now = *it;
  40. }
  41. if (now == -1 || now == ending) break;
  42. visit[now] = 1;
  43. for (int i = 0; i < st[now].next.size(); i++)
  44. {
  45. if (!visit[st[now].next[i].first] && (len[st[now].next[i].first] == -1 || len[st[now].next[i].first] > len[now] + 1))
  46. {
  47. len[st[now].next[i].first] = len[now] + 1;
  48. }
  49. }
  50. }
  51. }
  52. struct way
  53. {
  54. int s;
  55. int e;
  56. int line;
  57. way(int x, int y, int z) : s(x) , e(y) , line(z) {}
  58. };
  59. vector<vector<way> > res;
  60. vector<way> path;
  61. int min_cnt;
  62. void dfs(int now,int cur_cnt, int last,vector<way>& mytemp, int line)
  63. {
  64. //if (cur_cnt > len[ending]) return;
  65. if (cur_cnt > min_cnt) return;
  66. //if (now == ending && cur_cnt == len[ending])
  67. if (now == ending && cur_cnt <= min_cnt)
  68. {
  69. mytemp.push_back(way(last, ending, line));
  70. if (res.empty() || (mytemp.size() < res[0].size() && cur_cnt == min_cnt) || cur_cnt < min_cnt)
  71. {
  72. res.clear();
  73. res.push_back(mytemp);
  74. }
  75. min_cnt = cur_cnt;
  76. mytemp.pop_back();
  77. return;
  78. }
  79. visit[now] = 1;
  80. for (int i = 0; i < st[now].next.size(); i++)
  81. {
  82. if (!visit[st[now].next[i].first])
  83. {
  84. if (st[now].next[i].second != line && now != last)
  85. {
  86. mytemp.push_back(way(last, now, line));
  87. dfs(st[now].next[i].first, cur_cnt + 1, now, mytemp, st[now].next[i].second);
  88. mytemp.pop_back();
  89. }
  90. else
  91. {
  92. dfs(st[now].next[i].first, cur_cnt + 1, last, mytemp, st[now].next[i].second);
  93. }
  94. visit[st[now].next[i].first] = 0;
  95. }
  96. }
  97. }
  98. int main()
  99. {
  100. int n, m, id, last;
  101. scanf("%d", &n);
  102. for (int i = 1; i <= n; i++)
  103. {
  104. scanf("%d", &m);
  105. for(int j = 0; j < m; j++)
  106. {
  107. scanf("%d", &id);
  108. have.insert(id);
  109. st[id].val = id;
  110. if (j > 0)
  111. {
  112. st[id].next.push_back(make_pair(last,i));
  113. st[last].next.push_back(make_pair(id, i));
  114. }
  115. last = id;
  116. }
  117. }
  118. scanf("%d", &n);
  119. for (int i = 0; i < n; i++)
  120. {
  121. scanf("%d %d", &starting, &ending);
  122. //Dijkstra();
  123. min_cnt = have.size();
  124. memset(visit, 0, sizeof(visit));
  125. vector<way> temp;
  126. res.clear();
  127. dfs(starting,0,starting,temp,1);
  128. //printf("%d\n", len[ending]);
  129. printf("%d\n", min_cnt);
  130. for (int i = 0; i < res[0].size(); i++)
  131. {
  132. printf("Take Line#%d from %04d to %04d.\n", res[0][i].line,res[0][i].s,res[0][i].e);
  133. }
  134. }
  135. return 0;
  136. }

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. Javascript中的Callback方法浅析

    什么是callback?  回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数 ...

  2. linux常用命令总结->1

    文件查看命令 cat //查看文件内容 示例:cat /etc/passwd 示例:cat -n /etc/passwd //-n参数行号 示例:cat >> xuliangwei.txt ...

  3. python网络编程--线程锁(互斥锁Mutex)

    一:为什么需要线程锁 一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况? 很简单,假设你有A,B两 ...

  4. Hadoop(二):MapReduce程序(Java)

    Java版本程序开发过程主要包含三个步骤,一是map.reduce程序开发:第二是将程序编译成JAR包:第三使用Hadoop jar命令进行任务提交. 下面拿一个具体的例子进行说明,一个简单的词频统计 ...

  5. node练习笔记

    一.用http模块实现客户端 1.   这个错误的原因是:客户端http_client.js里面的端口和服务端里面的端口不一样 2.querystring.stringify  字符串转换成对象  q ...

  6. java基础79 会话管理(Cookie技术、Session技术)

    1.概念     会话管理:管理浏览器和服务器之间会话过程中产生的会话数据.    Cookie技术:会话数据保存到浏览器客户端.[存 编号/标记(id)]    Session技术:会话技术会保存到 ...

  7. tomcat数据源配置DBCP

    原文件: https://www.cnblogs.com/sicd/p/4053780.html DBCP object created 日期 by the following code was ne ...

  8. ubuntu14.04 使用传统的netcat

    Ubuntu上默认安装的是netcat-openbsd,而不是经典的netcat-traditional. 网上例子很多都是以netcat-traditional为例. sudo apt-get -y ...

  9. SQL中的注释语句

    SQL中的注释分为单行注释和多行注释.顾名思义,单行注释就是对一行进行注释,多行注释就是同时对多行进行注释. 一.单行注释 SQL语句中的单行注释使用 -- create database datab ...

  10. day5模块学习--configparser模块

       使用ConfigParser模块读写ini文件(http://blog.csdn.net/linda1000/article/details/11729561) ConfigParserPyth ...