题目链接:

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

题目描述:

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

样例说明:

思路:

1. 最短路径的一道题,要输出最短路径,Dijkstra+DFS, Dijkstra求最短路,DFS打印最短路。

2. 因为同时还要求最小花费,所以如果最短路条数不唯一,则还需要看当前路径是否花费更小。

3. 因为要打印出最短路径,所以要将S到达D点的前序路径都保存下来,再通过DFS深度遍历输出路径。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 505, inf = 0x3f3f3f3f;int G[maxn][maxn], d[maxn], c[maxn], cost[maxn][maxn], pre[maxn], minCost; // G邻接矩阵,d最短距离,cost花费,pre保存前序路径
int n, m, S, D;
bool vis[maxn]; // vis当前城市是否访问过void Dijkstra(int s)
{// 初始化d,cfill(d, d + maxn, inf);fill(c, c + maxn, inf);d[s] = 0; // 到自身的距离为0c[s] = 0; // 到自身的花费为0for (int i = 0; i < n; i++) { // 对每个城市都进行一次dijkstraint MIN = inf, u = -1;for (int j = 0; j < n; j++) { // 查找未访问过的城市中距离最小的if (!vis[j] && d[j] < MIN) {MIN = d[j];u = j;}}if (u == -1) // 未找到直接返回return;vis[u] = true; // 标记当前城市已访问for (int j = 0; j < n; j++) { // 遍历n个城市if (!vis[j] && G[u][j] != inf) { // 能直接到达且未访问过的城市if (d[u] + G[u][j] < d[j]) { // 如果当前路径到达的距离比之前的小d[j] = d[u] + G[u][j]; // 更新最短路径c[j] = c[u] + cost[u][j]; // 更新花费pre[j] = u; // 更新前序路径} else if (d[u] + G[u][j] == d[j]) { // 如果最短路不唯一if (c[j] > c[u] + cost[u][j]) { // 如果当前路径花费比之前的小c[j] = c[u] + cost[u][j]; // 更新花费pre[j] = u; // 更新前序路径}}}}}
}void DFS(int u) // 从终点开始递归打印路径
{if (u == S) { // 如果到达起点, 输出起点城市并返回printf("%d ", u);return;}DFS(pre[u]); // 否则递归查找前序路径printf("%d ", u); // 输出经过的城市
}int main()
{fill(G[0], G[0] + maxn * maxn, inf);cin >> n >> m >> S >> D;for (int i = 0; i < m; i++) {int a, b, dis, w;cin >> a >> b >> dis >> w;G[a][b] = G[b][a] = dis; // 距离赋值cost[a][b] = cost[b][a] = w; // 花费赋值}Dijkstra(S);DFS(D); // 打印路径printf("%d %d\n", d[D], c[D]); // 输出最短距离和最小花费return 0;
}

总结:

最短路题目的模板了,同时考虑最短路和最小花费,不管怎么样都先Dijkstra求最短路,然后求最短路的过程中保存最小花费的路径,最后递归打印出来。

1030 Travel Plan (30 分)相关推荐

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

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

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

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

  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 分) 【难度: 中 / 知识点: 最短路】

    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. 1030 Travel Plan(甲级)

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

  7. PAT甲级 1030 Travel Plan

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

  8. 1030 Travel Plan(超级无敌详细注释+47行代码)

    分数 30 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 A traveler's map gives the distances between cities along the ...

  9. 2020PAT甲级秋季7-4 Professional Ability Test (30分)

    7-4Professional Ability Test(30分) Professional Ability Test (PAT) consists of several series of subj ...

最新文章

  1. 微软发布通用型AI框架Avatar Framework
  2. android悬浮窗按钮在哪,android——悬浮窗控件Toast
  3. 《Java从入门到放弃》框架入门篇:hibernate基本配置
  4. 【Demo】改变SO项目状态并取消拒绝原因实现
  5. 牛客多校 - Minimum-cost Flow(最小费用最大流+贪心)
  6. 为什么很多大老板银行贷款几千万,看起来还那么潇洒?
  7. 戴文的Linux内核专题:07内核配置(3)
  8. java版AES加密算法实现
  9. html默认图片,web前端之网页中几种默认图片的解决方式
  10. logback日志模板
  11. 学习使用php简单读取pdf文件总页数的方法
  12. 火车头采集的数据库文件*.bd3是什么格式的数据库?
  13. CS院校解析 | 清华大学深圳国际研究生院
  14. 美化windows xp 完全教程
  15. 电脑wifi密码查看
  16. 200个句子搞定3500个高考词汇,究竟有多少词?
  17. Linux应用编程之时间编程
  18. HTML5、css3、js实现3D相册
  19. 使用python基于socket的tcp服务器聊天室
  20. Spring5学习详细笔记

热门文章

  1. 在Android Studio中使用tess-Two
  2. 【车】铅酸电池和锂电池、鼓刹和碟刹
  3. $(@:_config=)什么意思?
  4. 2022年样题五全国职业院校网络系统管理-网络部分
  5. 第一章:Getting Started
  6. Mplayer播放没有声音
  7. 石家庄计算机职业学院是本科吗,石家庄信息工程职业学院是本科还是专科?
  8. java毕业设计校园自行车租赁系统(附源码、数据库)
  9. 卡莱特led显示屏调试教程_|卡莱特LED显示屏校正软件(Calibration Pro)下载v3.5.0 官方版 - 欧普软件下载...
  10. 模电(十)结型场效应管