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. Day 4-2 random模块

    import random random.randint(1,100) # 从1到100中随机取出一个数.包含100 random.randrange(1,100) #功能和上面一样.只是不包含100 ...

  2. C# Note18: 使用wpf制作about dialog(关于对话框)

    前言 基本上任何software或application都会在help菜单中,有着一个关于对话框,介绍产品的版权.版本等信息,还有就是对第三方的引用(add author credits). 首先,看 ...

  3. cookie路径概念理解

    .创建一个cookie并设置 cookie的有效路径: $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' }); 注:在默认情况下 ...

  4. Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1)

    Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1) 准备工作: java:java 8 或者 java 9: Spring框架:5.0.8.RELEASE或以 ...

  5. ubunto启动chrome报错

    /usr/bin/google-chrome-stable[5199:5199:0703/143543.136117:ERROR:zygote_host_impl_linux.cc(88)] Runn ...

  6. Delphi 限制Edit输入 多个例子

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not (key in [ '.',#8]) then ...

  7. Python——Label控件说明

    Anchor :   标签中文本的位置: background(bg)foreground(fg) :背景色:前景色: borderwidth(bd) :边框宽度: width  .height   ...

  8. Lodop连续打印内容逐渐偏移怎么办

    Lodop打印控件中,可以使用打印机自带的纸张名称,也可以自定义纸张.(SET_PRINT_PAGESIZE语句).通常进行打印开发,为了避免浪费纸张,会用虚拟打印机效果作为依据,虚拟打印机连续打印多 ...

  9. easy install 与pip

    easy_insall的作用和perl中的cpan, ruby中的gem类似,都提供了在线一键安装模块的傻瓜方便方式,而pip是easy_install的改进版, 提供更好的提示信息,删除packag ...

  10. codeforces559B

    Equivalent Strings CodeForces - 559B Today on a lecture about strings Gerald learned a new definitio ...