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
 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int N, M, S, D;
const int INF = ;
int G[][], cost[][], visit[], dst[];
vector<int> pre[];
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i < N; i++){
int u = -, minLen = INF;
for(int j = ; j < N; j++){
if(visit[j] == && dst[j] < minLen){
minLen = dst[j];
u = j;
}
}
if(u == -){
return;
}
visit[u] = ;
for(int j = ; j < N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].push_back(u);
}
}
}
}
}
vector<int> tempPath, ans;
int minCost = INF;
void DFS(int d){
tempPath.push_back(d);
if(d == S){
int tempCost = ;
for(int i = tempPath.size() - ; i > ; i--){
tempCost += cost[tempPath[i]][tempPath[i - ]];
}
if(tempCost < minCost){
minCost = tempCost;
ans = tempPath;
}
tempPath.pop_back();
return;
}
int len = pre[d].size();
for(int i =; i < len; i++){
DFS(pre[d][i]);
}
tempPath.pop_back();
}
int main(){
fill(G[], G[] + *, INF);
scanf("%d%d%d%d", &N, &M, &S, &D);
int tempA, tempB, tempD, tempC;
for(int i = ; i < M; i++){
scanf("%d%d%d%d", &tempA, &tempB, &tempD, &tempC);
G[tempA][tempB] = G[tempB][tempA] = tempD;
cost[tempA][tempB] = cost[tempB][tempA] = tempC;
}
dijkstra(S);
DFS(D);
for(int i = ans.size() - ; i >= ; i--){
printf("%d ", ans[i]);
}
printf("%d %d", dst[D], minCost);
cin >> N;
return ;
}

总结:

1、题意:求出最短路径,如果有多条则求出花费的边权之和最短的一条。本题可采用dijkstra专门搜索最短路,将其存入pre数组。再用深搜遍历pre,计算、比较cost的花费并保存最优路径。

2、迪杰斯特拉:注意每次选择一个未优化的且距离最短的节点作为u,并将其visit设为1。注意在选择更新的节点也要是未被优化过的节点。 注意别忘记更新dst。

3、深搜注意push和pop的位置和时机。

A1030. Travel Plan的更多相关文章

  1. PAT甲级——A1030 Travel Plan

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

  2. PAT_A1030#Travel Plan

    Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...

  3. 1030 Travel Plan (30 分)

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

  4. PAT1030 Travel Plan (30)---DFS

    (一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...

  5. PAT 1030 Travel Plan[图论][难]

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

  6. [图算法] 1030. Travel Plan (30)

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

  7. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  8. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

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

  9. HDU 4014 Jimmy’s travel plan(图计数)

    Jimmy’s travel plan Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

随机推荐

  1. Spark源码编译,官网学习

    这里以spark-1.6.0版本为例 官网网址   http://spark.apache.org/docs/1.6.0/building-spark.html#building-with-build ...

  2. python 获取列表中次大的数值.

    需求: 1.写个函数,把一组数字传到函数中,然后取出最大值和次大值. 2.不能使用排序函数. 分析: Q: list = [100,50,60,70,30,45] 怎么从这个列表中取出最大值? A: ...

  3. 校园电商项目3(基于SSM)——配置Maven

    步骤一:添加必要文件夹 先在src/main/resources下添加两个文件夹 接着在webapp文件夹下添加一个resources文件夹存放我们的静态网页内容 WEB-INF里的文件是不会被客户端 ...

  4. WorldCount代码检查与优化——软件测试第三次作业

    合作者:201631062222,201631062232 代码地址:https://gitee.com/biubiubiuLYQ/ceshi_secend 本次作业链接地址:https://edu. ...

  5. C# 中那些常用的工具类(Utility Class)(三)

    今天来接着写这个系列的文章,这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候 ...

  6. CSS3圆角详解(border-radius)

    1.CSS3圆角的优点 传统的圆角生成方案,必须使用多张图片作为背景图案.CSS3的出现,使得我们再也不必浪费时间去制作这些图片了,而且还有其他多个优点: 减少维护的工作量.图片文件的生成.更新.编写 ...

  7. git ignore 忽略 idea文件

    下载了项目组的代码之后发现,一个问题,一编译就生成了很多的 .idea文件夹 还有 target文件夹,这些是不需要提交到git上的, 需要提交的时候屏蔽一下,所以需要建立一个ignore文件列表把他 ...

  8. ubuntu 完全卸载mysql

    卸载 sudo apt-get --purge remove mysql-common -y sudo apt-get --purge remove mysql* -y sudo apt-get au ...

  9. jvm相关参数

    官方地址:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html#BEHHGFAE 一.查看jvm运行参数 1.查看 ...

  10. Sql Server 时间格式化

    0   或   100   (*)     默认值   mon   dd   yyyy   hh:miAM(或   PM)       1   101   美国   mm/dd/yyyy       ...