这题在搞清楚思路绕过坑后,还是可以写的出通过sample data的代码的。但是不能AC,让我很气。

最后查清原因:还是对dfs本质理解的不够。

wa代码:

vis[s]=1;
dfs(s,e,0);

殊不知本题有多个查询数据。如果只调用一遍还可以蒙混过关,但是这样的错误必然导致wa

ac代码:

vis[s]=1;
dfs(s,e,0);
vis[s]=0;

参考柳诺博客修改的AC代码:

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <math.h>
  4. #include <string>
  5. #include <vector>
  6. #include <set>
  7. #include <stack>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <map>
  11.  
  12. #define I scanf
  13. #define OL puts
  14. #define O printf
  15. #define F(a,b,c) for(a=b;a<c;a++)
  16. #define FF(a,b) for(a=0;a<b;a++)
  17. #define FG(a,b) for(a=b-1;a>=0;a--)
  18. #define LEN 10000
  19. #define MAX 0x06FFFFFF
  20. #define V vector<int>
  21.  
  22. using namespace std;
  23.  
  24. vector<int> g[LEN];
  25. int line[LEN][LEN];
  26. int vis[LEN];
  27. vector<int> path;
  28. vector<int> ans;
  29. int min_d=MAX;
  30. int min_ls=MAX;
  31.  
  32. int N,M,K;
  33.  
  34. int calc_ls(){
  35. int cnt=-,preLine=;
  36. for(int i=;i<path.size();i++){
  37. if(line[path[i-]][path[i]]!=preLine) cnt++;
  38. preLine=line[path[i-]][path[i]];
  39. }
  40. return cnt;
  41. }
  42.  
  43. void dfs(int s,int e,int d){
  44. if(s==e){
  45. int ls=calc_ls();
  46. if(d<min_d || (d==min_d && ls<min_ls)){
  47. min_d=d;
  48. min_ls=ls;
  49. ans=path;
  50. }
  51. return;
  52. }
  53. int i;
  54. FF(i,g[s].size()){
  55. int o=g[s][i];
  56. if(!vis[o]){
  57. vis[o]=;
  58. path.push_back(o);
  59. dfs(o,e,d+);
  60. vis[o]=;
  61. path.pop_back();
  62. }
  63. }
  64.  
  65. }
  66.  
  67. void printLine(){
  68. int s=ans[];
  69. int preL=;
  70. int i;
  71. F(i,,ans.size()){
  72. if(line[ans[i-]][ans[i]]!=preL){
  73. if(preL) printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
  74. s=ans[i-];
  75. preL=line[ans[i-]][ans[i]];
  76. }
  77. }
  78. printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
  79. }
  80.  
  81. int main(){
  82. // freopen("1131.txt","r",stdin);
  83. int s,e,i,j;
  84. I("%d",&N);
  85. F(i,,N+){
  86. int pre,p=-;
  87. I("%d",&M);
  88. while(M--){
  89. pre=p;
  90. I("%d",&p);
  91. if(pre>=){
  92. g[p].push_back(pre);
  93. g[pre].push_back(p);
  94. line[p][pre]=i;
  95. line[pre][p]=i;
  96. }
  97. }
  98. }
  99. I("%d",&K);
  100. while(K--){
  101. min_d=MAX;
  102. min_ls=MAX;
  103. path.clear();
  104. ans.clear();
  105. I("%d%d",&s,&e);
  106. path.push_back(s);
  107. vis[s]=;
  108. dfs(s,e,);
  109. vis[s]=;
  110. O("%d\n",min_d);
  111. printLine();
  112. }
  113. return ;
  114. }

在自己思路上修改的AC代码:(个人认为比柳诺的好理解)

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <math.h>
  4. #include <string>
  5. #include <vector>
  6. #include <set>
  7. #include <stack>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <map>
  11.  
  12. #define I scanf
  13. #define OL puts
  14. #define O printf
  15. #define F(a,b,c) for(a=b;a<c;a++)
  16. #define FF(a,b) for(a=0;a<b;a++)
  17. #define FG(a,b) for(a=b-1;a>=0;a--)
  18. #define LEN 10000
  19. #define MAX 0x06FFFFFF
  20. #define V vector<int>
  21.  
  22. using namespace std;
  23.  
  24. vector<int> g[LEN];
  25. int line[LEN][LEN];
  26. int vis[LEN];
  27. vector<int> path;
  28. vector<int> ans;
  29. int min_d=MAX;
  30. int min_ls=MAX;
  31.  
  32. int N,M,K;
  33.  
  34. void dfs(int s,int e,int d,int l,int ls){
  35. if(s==e){
  36. if(d<min_d || (d==min_d && ls<min_ls)){
  37. min_d=d;
  38. min_ls=ls;
  39. ans=path;
  40. }
  41. return;
  42. }
  43. int i;
  44. FF(i,g[s].size()){
  45. int o=g[s][i];
  46. if(!vis[o]){
  47. vis[o]=;
  48. path.push_back(o);
  49. int nl=line[s][o];
  50. int nls=ls;
  51. if(l==){ //初始结点
  52. nls=;
  53. }else{
  54. if(nl!=l) nls++;
  55. }
  56. dfs(o,e,d+,nl,nls);
  57. vis[o]=;
  58. path.pop_back();
  59. }
  60. }
  61.  
  62. }
  63.  
  64. void printLine(){
  65. int s=ans[];
  66. int preL=line[s][ans[]];
  67. int i;
  68. F(i,,ans.size()){
  69. if(line[ans[i-]][ans[i]]!=preL){
  70. printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
  71. s=ans[i-];
  72. preL=line[ans[i-]][ans[i]];
  73. }
  74. }
  75. printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
  76. }
  77.  
  78. int main(){
  79. // freopen("1131.txt","r",stdin);
  80. int s,e,i,j;
  81. I("%d",&N);
  82. F(i,,N+){
  83. int pre,p=-;
  84. I("%d",&M);
  85. while(M--){
  86. pre=p;
  87. I("%d",&p);
  88. if(pre>=){
  89. g[p].push_back(pre);
  90. g[pre].push_back(p);
  91. line[p][pre]=i;
  92. line[pre][p]=i;
  93. }
  94. }
  95. }
  96. I("%d",&K);
  97. while(K--){
  98. min_d=MAX;
  99. min_ls=MAX;
  100. path.clear();
  101. ans.clear();
  102. I("%d%d",&s,&e);
  103. vis[s]=;
  104. dfs(s,e,,,);
  105. vis[s]=;
  106. O("%d\n",ans.size());
  107. ans.insert(ans.begin(),s);
  108. printLine();
  109. }
  110. return ;
  111. }

注意点:

① 38 39 行,对维护的最小距离和最小换乘次数进行更新,不要写错(我开始写成了d=min_d ,查了很久的错,蠢哭……)

② 99 100 行,将最小距离和最小换乘次数重新初始化为INF。

③ 106 行,牢记 dfs 结构

图的遍历 | 1131地铁图: dfs复杂模拟题的更多相关文章

  1. 图的遍历(bfs 和dfs)

    BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...

  2. 图的遍历 之 深搜dfs

    DFS 遍历 深度优先搜索是一个递归过程,有回退过程. 对一个无向连通图,在访问图中某一起始顶点u 后,由u 出发,访问它的某一邻接顶点v1:再从v1 出发,访问与v1 邻接但还没有访问过的顶点v2: ...

  3. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  4. PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

    题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...

  5. 洛谷P3916||图的遍历||反向建图||链式前向星||dfs

    题目描述 给出 NN 个点, MM 条边的有向图,对于每个点 vv ,求 A(v)A(v) 表示从点 vv 出发,能到达的编号最大的点. 解题思路 看起来很简单的一道题, 但我依然调了一天,我还是太菜 ...

  6. Luogu P3916 图的遍历 【优雅的dfs】【内有待填坑】By cellur925

    说明 • 对于60% 的数据, n,m在1e3内 • 对于100% 的数据, n,m在1e5内. 本弱弱上来就是一顿暴搜打,dfs n次,每次更新答案,复杂度为O(n*n),果然TLE,60分抱回家. ...

  7. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  8. 图的遍历——DFS(矩形空间)

    首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...

  9. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

随机推荐

  1. UML类图记忆口诀

    UML类图在设计模式书籍中用的比较多,经常忘记,口诀挺重要的,比如我们从小到大,除了乘法口诀.元素周期表等口诀形式的知识,其它的知识都基本忘记了, 所以编写口诀如下 1.三级石 2.见关一 3.零足迹 ...

  2. Mysql load data infile 命令导入含中文csv源数据文件 【错误代码 1300】

    [1]Load data infile 命令导入含中文csv源数据文件 报错:Invalid utf8 character string: '??֧' (1)问题现象 csv格式文件源数据: 导入SQ ...

  3. Https通信原理及Android中实用总结

    一.背景 Http俨然已经成为互联网上最广泛使用的应用层协议,随着应用形态的不断演进,传统的Http在安全性上开始面临挑战,Http主要安全问题体现在: 1,信息内容透明传输. 2,通信对方的身份不可 ...

  4. java设计模式结构型模式

    结构型模式: – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结 构,用来解决更大的问题 分类: • 适配器模式.代理模式.桥接模式. 装饰模式.组合模式.外观模式.享元模式 结构型模式 ...

  5. java设计模式单例模式

    创建型模式: – 单例模式.工厂模式.抽象工厂模式.建造者模式.原型模式. • 结构型模式: – 适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模 式. • 行为型模式: – 模版 ...

  6. WPF WebBrowser抑制Suppress 弹出 脚本错误 对话框 但是样式改变 需要继续改善

    1.添加引用 using System.Reflection;using System.Windows.Controls; 2.静态类扩展方法(this) public static class We ...

  7. 在 VSCode 调试过程中,使用 Watcher,免手动重新编译

    1.安装Microsoft.DotNet.Watcher.Tools包 dotnet add package Microsoft.DotNet.Watcher.Tools --version 2.0. ...

  8. json解析常见异常

    (1) : org.json.JSONException: Expected a ',' or '}' at 80 [character 81 line 1]   原因:出现乱码了, 导致json格式 ...

  9. HDU2023求平均成绩 - biaobiao88

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2023 求平均成绩 Problem Description 假设一个班有n(n<=50)个学生,每 ...

  10. HTML中的音频 视频 的播放代码

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...