(一)题意

题目链接:https://www.patest.cn/contests/pat-a-practise/1030

1030. Travel Plan (30)

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output

0 2 3 3 40——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————(二)题解这道题是PAT最很喜欢出的一道求最短路的变形题。解法有很多种,但鉴于我做上一道LeetCode花费了过多的经历,而且还要面对导师催论文的压力,我只写一种解法。直接用DFS寻找从源点到目的地的所有路径中距离最短且花费最少的路径,用path记录路径。DFS很方便可以记录路径,是借助深度deep作为路径下标。代码如下:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define maxn 510
 4 #define For(I,A,B) for(int I = (A); I < (B); I++)
 5 int n,m,s,d;
 6 bool vis[maxn];
 7 int dist[maxn][maxn];
 8 int cost[maxn][maxn];
 9 int cur_path[maxn],path[maxn];
10 int mindis,minc,len;
11
12 void dfs(int node,int dis,int cc,int deep)
13 {
14     cur_path[deep] = node;
15     if(node == d)
16     {
17         if(dis < mindis)
18         {
19             mindis = dis;
20             minc = cc;
21             len = deep;
22             For(i,0,deep + 1)
23                 path[i] = cur_path[i];
24         }
25         else if (dis == mindis && cc < minc)
26         {
27             mindis = dis;
28             minc = cc;
29             len = deep;
30             For(i,0,deep + 1)
31                 path[i] = cur_path[i];
32         }
33         return;
34     }
35     For(i,0,n)
36     {
37         if(!vis[i] && dist[i][node] != -1)
38         {
39             vis[i] = 1;
40             dfs(i,dis + dist[i][node],cc + cost[node][i],deep + 1);
41             vis[i] = 0;
42         }
43     }
44 }
45 int main()
46 {
47     //freopen("1030.in","r",stdin);
48     while(scanf("%d%d%d%d",&n,&m,&s,&d) != EOF)
49     {
50         int t1,t2;
51         mindis = minc = INT_MAX;
52         For(i,0,n)
53             For(j,0,n)
54             dist[i][j] = -1;
55         For(i,0,m)
56         {
57             scanf("%d%d",&t1,&t2);
58             scanf("%d%d",&dist[t1][t2],&cost[t1][t2]);
59             dist[t2][t1] = dist[t1][t2];
60             cost[t2][t1] = cost[t1][t2];
61         }
62         vis[s] = 1;
63         dfs(s,0,0,0);
64         For(i,0,len + 1)
65             cout<<path[i]<<" ";
66         cout<<mindis<<" "<<minc<<endl;
67     }
68     return 0;
69 }

相似的题目还包括PAT1018(https://www.patest.cn/contests/pat-a-practise/1018)和PAT1003(https://www.patest.cn/contests/pat-a-practise/1003)

转载于:https://www.cnblogs.com/xiaozhuyang/p/6119865.html

PAT1030 Travel Plan (30)---DFS相关推荐

  1. 【PAT】【spfa + dfs】1030 Travel Plan (30 分)

    题目链接:1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, ...

  2. PAT甲级1030 Travel Plan (30分):[C++题解]dijkstra求单源最短路、保存路径

    文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析 dijkstra模板默写过来,然后多了一个保存路径,使用数组pre[N]记录最短路上每个点的前驱,通过pre数组保存到vector中 v ...

  3. 【讲解】1030 Travel Plan (30 分)【DFS】_41行代码Ac

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A traveler's map gives the distances between cities along the hig ...

  4. 1030 Travel Plan (30 分)

    题目链接: PTA | 程序设计类实验辅助教学平台千名教师建设,万道高质量题目,百万用户拼题的程序设计实验辅助教学平台https://pintia.cn/problem-sets/9948053427 ...

  5. 1030 Travel Plan (30 分) 【难度: 中 / 知识点: 最短路】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 先跑一下Dijkstra() 然后再dfs( ...

  6. 1030 Travel Plan (30分)(俺是个粗人)

    就保存所有的路径,找出最短最便宜的叭 #include<iostream> #include<vector> #include<limits.h> using na ...

  7. 1030 Travel Plan(甲级)

    1030 Travel Plan (30分) A traveler's map gives the distances between cities along the highways, toget ...

  8. PAT甲级 1030 Travel Plan

    PAT甲级 1030 Travel Plan 题目链接 A traveler's map gives the distances between cities along the highways, ...

  9. 以未来式计算机为题目的作文,一般将来时:my travel plan为题作文

    满意答案 z4255621 2016.06.12 采纳率:43%    等级:12 已帮助:10511人 找了两则,供您参考.希望有所帮助哈. My Travel Plan--01 I have a ...

  10. PAT 1131. Subway Map (30) DFS

    1131. Subway Map (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In the big ...

最新文章

  1. Linux 内核中断体系 初探
  2. 双色球随机选号器界面设计及功能实现
  3. 3级联动 ajax java_ajax实现三级联动的基本方法
  4. Redis-学习笔记06【Redis案例】
  5. 操作系统内核的一些事
  6. python os模块下载_python os模块
  7. POJ 3734 Blocks 矩阵递推
  8. 二进制安装mariadb 10.2.16
  9. html怎么只操作第一个li,css3如何选择第一个子元素?
  10. 关于Bean Validation
  11. C#总结项目《影院售票系统》编写总结一
  12. Git基本操作(学习笔记)
  13. 光纤色散是什么?如何色散补偿?
  14. 在Excel中如何提取括号中的数字
  15. e4e反演框架:Designing an Encoder for StyleGAN Image Manipulation
  16. 10Wqps评论中台,如何架构?B站是这么做的!!!
  17. Matlab2020a安装
  18. display常用属性值
  19. Python-matplotlib plt.tick_params参数解析
  20. 如何配置Python虚拟环境

热门文章

  1. python绘制小狗_用Python画一只有点方的小狗狗——turtle库基础入门
  2. 30天自制操作系统之叠加处理
  3. 暴力枚举 --- 8.1 Subsets --- 图解
  4. CACHECLOUDV1.0慢日志定时任务创建流程分析
  5. Mac使用VMware、Ubuntu安装配置虚拟机Linux
  6. Linux 软件安装目录详解
  7. python中pickle模块_python标准库学习之pickle模块
  8. MyBatis的XML配置文件(三)
  9. ubuntu16.04下ROS操作系统学习笔记(二)命令工具了解和仿真小海龟
  10. 协同过滤相关算法(1):SVD