立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


采用vector做dfs的参数,同时求出最短距离和路径。 具体逻辑请读者去代码中体会。


#include<bits/stdc++.h>
using namespace std;int G[510][510], vis[510], cost[510][510];  //地图、vis数组、走每条路的花费成本
int n, m, s_c, d_c;                         //城市数、道路数、起点、终点
vector<int>fin;                               //存储最终路线
int min_dis = 0x3f3f3f3f, min_cost = 0x3f3f3f3f;  //最短距离和花费 void dfs(int now_dis, int now_cost, vector<int>v) {int s = v.back();                       //最后一个元素为当前位置 if(now_dis > min_dis) return;          //剪枝if(s == d_c) {                            //边界条件 if(now_dis == min_dis)                 //距离相同则比较花费 if(now_cost > min_cost) return;min_dis = now_dis; min_cost = now_cost;fin = v;return; }for(int i = 0; i < n; i++)               //逐个点遍历 if(vis[i] == 0 && G[s][i]>0) {     //如果没访问过且连通v.push_back(i);              //v数组压入该点 vis[i] = 1;                      //置1表示已访问 dfs(now_dis+G[s][i], now_cost+cost[s][i], v);vis[i] = 0;                       //回溯 v.pop_back();}
}int main() {cin >> n >> m >> s_c >> d_c;for(int i = 0; i < m; i++) {int c1, c2, dis, val; cin >> c1 >> c2 >> dis >> val;G[c1][c2] = G[c2][c1] = dis;       //构建地图和距离 cost[c1][c2] = cost[c2][c1] = val;  //花费 }vector<int>v;  v.push_back(s_c);    //压入起点 vis[s_c] = 1;dfs(0, 0, v);  //距离、花费、路线 for(int i = 0; i < fin.size(); i++) cout << fin[i] << ' ';cout << min_dis << ' ' << min_cost << '\n';return 0;
}

耗时:

【讲解】1030 Travel Plan (30 分)【DFS】_41行代码Ac相关推荐

  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 分)

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

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

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

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

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

  6. (~解题报告~)L1-017 到底有多二 (15分)(16行代码AC)

    立志用更少的代码做更高效的表达 一个整数"犯二的程度"定义为该数字中包含2的个数与其位数的比值.如果这个数是负数,则程度增加0.5倍:如果还是个偶数,则再增加1倍.例如数字-131 ...

  7. (~解题报告~)L1-016 查验身份证 (15分)(29行代码AC!)

    立志用更少的代码做更优化的表达 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下:   首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2 ...

  8. L1-007 念数字 (10分) (13行代码AC!!)

    立志用更少的代码做更高效的表达 输入一个整数,输出每个数字对应的拼音.当整数为负数时,先输出fu字.十个数字对应的拼音如下: 0: ling 1: yi 2: er 3: san 4: si 5: w ...

  9. 1030 Travel Plan(甲级)

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

最新文章

  1. OpenJDK官方正式宣布AWT、2D、Swing等项目解散
  2. 数据挖掘Apriori算法
  3. 完美世界手游不显示新服务器,完美世界手游手Q互通-黑曜开服时间表_完美世界手游新区开服预告_第一手游网手游开服表...
  4. c#中去掉字符串空格方法
  5. 后端:Java中如何更优雅的处理空值,看完你就懂了!
  6. scrapyd远程连接配置
  7. (13)Node.js 文件流 缓冲 VS 流
  8. SAP License:关于SAP 对生产订单的月度结算
  9. 5 张图带你了解 Pulsar 的存储引擎 BookKeeper
  10. 线程 daemon java user,Java线程_守护线程和用户线程
  11. 联众打码写滑动_自己写了一个答题的软件现在分享一下(在分享一下联众打码源码调用) _ 综合讨论 - 按键精灵论坛...
  12. linux计时器命令,安装及使用Linux终端倒数计时器Countdown的方法
  13. 正激电路:半桥(波形解释)
  14. 解决Ubuntu18.04无法安装旧版本gfortran4.6
  15. Countdownlatch、CyclicBarrier、join区别
  16. leetcode 第1题【两数之和】C语言
  17. 做大数据工程师,需要学习什么?
  18. 基数排序——多关键字排序(MSD/LSD)以及链式基数排序
  19. Nagios-安装与配置
  20. Arduino 入门学习笔记7 I2C LCD1602液晶显示实验 及 超声波传感器距离检测

热门文章

  1. 记一次失败的Windows环境编译Nginx源码
  2. 80%的Linux都不懂的内存问题
  3. 全球编程语言薪资排行榜,Java竟然垫底!!!
  4. 工具类用得好,下班下的早
  5. 12种提升视频质量的方法
  6. LiveVideoStackCon讲师热身分享 ( 十四 ) —— HEVC标准在客户端上的快速实现方法的研究...
  7. 章琦:能坚持的唯一的原因就是兴趣
  8. Java多线程之CAS缺点
  9. 聚集云原生,可观测性的实践与探索 | 线下技术沙龙
  10. OCP大会 | T-Flex 2.0服务器框架介绍(附PDF)