图的遍历 | 1131地铁图: dfs复杂模拟题
这题在搞清楚思路绕过坑后,还是可以写的出通过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代码:
- #include <stdio.h>
- #include <memory.h>
- #include <math.h>
- #include <string>
- #include <vector>
- #include <set>
- #include <stack>
- #include <queue>
- #include <algorithm>
- #include <map>
- #define I scanf
- #define OL puts
- #define O printf
- #define F(a,b,c) for(a=b;a<c;a++)
- #define FF(a,b) for(a=0;a<b;a++)
- #define FG(a,b) for(a=b-1;a>=0;a--)
- #define LEN 10000
- #define MAX 0x06FFFFFF
- #define V vector<int>
- using namespace std;
- vector<int> g[LEN];
- int line[LEN][LEN];
- int vis[LEN];
- vector<int> path;
- vector<int> ans;
- int min_d=MAX;
- int min_ls=MAX;
- int N,M,K;
- int calc_ls(){
- int cnt=-,preLine=;
- for(int i=;i<path.size();i++){
- if(line[path[i-]][path[i]]!=preLine) cnt++;
- preLine=line[path[i-]][path[i]];
- }
- return cnt;
- }
- void dfs(int s,int e,int d){
- if(s==e){
- int ls=calc_ls();
- if(d<min_d || (d==min_d && ls<min_ls)){
- min_d=d;
- min_ls=ls;
- ans=path;
- }
- return;
- }
- int i;
- FF(i,g[s].size()){
- int o=g[s][i];
- if(!vis[o]){
- vis[o]=;
- path.push_back(o);
- dfs(o,e,d+);
- vis[o]=;
- path.pop_back();
- }
- }
- }
- void printLine(){
- int s=ans[];
- int preL=;
- int i;
- F(i,,ans.size()){
- if(line[ans[i-]][ans[i]]!=preL){
- if(preL) printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
- s=ans[i-];
- preL=line[ans[i-]][ans[i]];
- }
- }
- printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
- }
- int main(){
- // freopen("1131.txt","r",stdin);
- int s,e,i,j;
- I("%d",&N);
- F(i,,N+){
- int pre,p=-;
- I("%d",&M);
- while(M--){
- pre=p;
- I("%d",&p);
- if(pre>=){
- g[p].push_back(pre);
- g[pre].push_back(p);
- line[p][pre]=i;
- line[pre][p]=i;
- }
- }
- }
- I("%d",&K);
- while(K--){
- min_d=MAX;
- min_ls=MAX;
- path.clear();
- ans.clear();
- I("%d%d",&s,&e);
- path.push_back(s);
- vis[s]=;
- dfs(s,e,);
- vis[s]=;
- O("%d\n",min_d);
- printLine();
- }
- return ;
- }
在自己思路上修改的AC代码:(个人认为比柳诺的好理解)
- #include <stdio.h>
- #include <memory.h>
- #include <math.h>
- #include <string>
- #include <vector>
- #include <set>
- #include <stack>
- #include <queue>
- #include <algorithm>
- #include <map>
- #define I scanf
- #define OL puts
- #define O printf
- #define F(a,b,c) for(a=b;a<c;a++)
- #define FF(a,b) for(a=0;a<b;a++)
- #define FG(a,b) for(a=b-1;a>=0;a--)
- #define LEN 10000
- #define MAX 0x06FFFFFF
- #define V vector<int>
- using namespace std;
- vector<int> g[LEN];
- int line[LEN][LEN];
- int vis[LEN];
- vector<int> path;
- vector<int> ans;
- int min_d=MAX;
- int min_ls=MAX;
- int N,M,K;
- void dfs(int s,int e,int d,int l,int ls){
- if(s==e){
- if(d<min_d || (d==min_d && ls<min_ls)){
- min_d=d;
- min_ls=ls;
- ans=path;
- }
- return;
- }
- int i;
- FF(i,g[s].size()){
- int o=g[s][i];
- if(!vis[o]){
- vis[o]=;
- path.push_back(o);
- int nl=line[s][o];
- int nls=ls;
- if(l==){ //初始结点
- nls=;
- }else{
- if(nl!=l) nls++;
- }
- dfs(o,e,d+,nl,nls);
- vis[o]=;
- path.pop_back();
- }
- }
- }
- void printLine(){
- int s=ans[];
- int preL=line[s][ans[]];
- int i;
- F(i,,ans.size()){
- if(line[ans[i-]][ans[i]]!=preL){
- printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
- s=ans[i-];
- preL=line[ans[i-]][ans[i]];
- }
- }
- printf("Take Line#%d from %04d to %04d.\n",preL,s,ans[i-]);
- }
- int main(){
- // freopen("1131.txt","r",stdin);
- int s,e,i,j;
- I("%d",&N);
- F(i,,N+){
- int pre,p=-;
- I("%d",&M);
- while(M--){
- pre=p;
- I("%d",&p);
- if(pre>=){
- g[p].push_back(pre);
- g[pre].push_back(p);
- line[p][pre]=i;
- line[pre][p]=i;
- }
- }
- }
- I("%d",&K);
- while(K--){
- min_d=MAX;
- min_ls=MAX;
- path.clear();
- ans.clear();
- I("%d%d",&s,&e);
- vis[s]=;
- dfs(s,e,,,);
- vis[s]=;
- O("%d\n",ans.size());
- ans.insert(ans.begin(),s);
- printLine();
- }
- return ;
- }
注意点:
① 38 39 行,对维护的最小距离和最小换乘次数进行更新,不要写错(我开始写成了d=min_d ,查了很久的错,蠢哭……)
② 99 100 行,将最小距离和最小换乘次数重新初始化为INF。
③ 106 行,牢记 dfs 结构
图的遍历 | 1131地铁图: dfs复杂模拟题的更多相关文章
- 图的遍历(bfs 和dfs)
BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...
- 图的遍历 之 深搜dfs
DFS 遍历 深度优先搜索是一个递归过程,有回退过程. 对一个无向连通图,在访问图中某一起始顶点u 后,由u 出发,访问它的某一邻接顶点v1:再从v1 出发,访问与v1 邻接但还没有访问过的顶点v2: ...
- 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 ...
- 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 ...
- 洛谷P3916||图的遍历||反向建图||链式前向星||dfs
题目描述 给出 NN 个点, MM 条边的有向图,对于每个点 vv ,求 A(v)A(v) 表示从点 vv 出发,能到达的编号最大的点. 解题思路 看起来很简单的一道题, 但我依然调了一天,我还是太菜 ...
- Luogu P3916 图的遍历 【优雅的dfs】【内有待填坑】By cellur925
说明 • 对于60% 的数据, n,m在1e3内 • 对于100% 的数据, n,m在1e5内. 本弱弱上来就是一顿暴搜打,dfs n次,每次更新答案,复杂度为O(n*n),果然TLE,60分抱回家. ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 图的遍历——DFS(矩形空间)
首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...
- 图的遍历——DFS和BFS模板(一般的图)
关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...
随机推荐
- UML类图记忆口诀
UML类图在设计模式书籍中用的比较多,经常忘记,口诀挺重要的,比如我们从小到大,除了乘法口诀.元素周期表等口诀形式的知识,其它的知识都基本忘记了, 所以编写口诀如下 1.三级石 2.见关一 3.零足迹 ...
- Mysql load data infile 命令导入含中文csv源数据文件 【错误代码 1300】
[1]Load data infile 命令导入含中文csv源数据文件 报错:Invalid utf8 character string: '??֧' (1)问题现象 csv格式文件源数据: 导入SQ ...
- Https通信原理及Android中实用总结
一.背景 Http俨然已经成为互联网上最广泛使用的应用层协议,随着应用形态的不断演进,传统的Http在安全性上开始面临挑战,Http主要安全问题体现在: 1,信息内容透明传输. 2,通信对方的身份不可 ...
- java设计模式结构型模式
结构型模式: – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结 构,用来解决更大的问题 分类: • 适配器模式.代理模式.桥接模式. 装饰模式.组合模式.外观模式.享元模式 结构型模式 ...
- java设计模式单例模式
创建型模式: – 单例模式.工厂模式.抽象工厂模式.建造者模式.原型模式. • 结构型模式: – 适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模 式. • 行为型模式: – 模版 ...
- WPF WebBrowser抑制Suppress 弹出 脚本错误 对话框 但是样式改变 需要继续改善
1.添加引用 using System.Reflection;using System.Windows.Controls; 2.静态类扩展方法(this) public static class We ...
- 在 VSCode 调试过程中,使用 Watcher,免手动重新编译
1.安装Microsoft.DotNet.Watcher.Tools包 dotnet add package Microsoft.DotNet.Watcher.Tools --version 2.0. ...
- json解析常见异常
(1) : org.json.JSONException: Expected a ',' or '}' at 80 [character 81 line 1] 原因:出现乱码了, 导致json格式 ...
- HDU2023求平均成绩 - biaobiao88
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2023 求平均成绩 Problem Description 假设一个班有n(n<=50)个学生,每 ...
- HTML中的音频 视频 的播放代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...