A1030. Travel Plan
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的更多相关文章
- PAT甲级——A1030 Travel Plan
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- PAT_A1030#Travel Plan
Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- PAT1030 Travel Plan (30)---DFS
(一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- 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 ...
- HDU 4014 Jimmy’s travel plan(图计数)
Jimmy’s travel plan Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
随机推荐
- jenkins配置SSH远程服务器连接
之前用jenkins做了一个自动发布测试,配置任务的Post Steps时,选择的是执行shell命令.如下图: 这是在本192.168.26.233服务器上测试的,此服务器上运行jenkins,to ...
- Android常用的技术框架与博客社区
技术框架 图片加载 Glide Fresco Volley Picasso Universal Image Loader 网络请求 okhttp retrofit Volley android-asy ...
- git(命令行常用炒作)
Git常用操作 https://backlog.com/git-tutorial/cn/intro/intro1_1.html Git详解(思维导图) https://blog.csdn.net/hu ...
- python学习 第二天
一.变量 1.变量名: 数字,字母,下划线 alex1=123 sb=“alex” a_lex=“sb” 不能以数字开头 lalex 变量名不是python内部的关键字 {‘and’,'as','as ...
- 原型链上的call方法集合
1. Object.prototype.toString.call(value) // 返回数据的类型 // "[object Object]" 等 2. Array.protot ...
- Base64 总结
Base64编码是解决一些无法打印的字符无法显示的问题,将8位的ascii编码转换为6位的表示64个可见字符的算法. 具体而言,首先将编码每三个分成一组,将三个字符转换为总长为24位的二进制 数字,将 ...
- 进程间通信IPC与Binder机制原理
1, Intent隐式意图携带数据 2, AIDL(Binder) 3, 广播BroadCast 4, 内容提供者ContentProvider 5,Messager(内部通过binder实现) 6, ...
- ab与nc命令,tcpdump命令
ab与nc命令,tcpdump命令 ab -p post.txt -T application/json "http://127.0.0.1:8083/main/index&quo ...
- kubernetes 编排详解 资源分配
########给pod 分配cpu和内存资源apiVersion: v1 kind: Pod metadata: name: frontend spec: containers: - name: d ...
- Codeforces Round #507 Div. 1
D:类似于noip2018d1t3,子树内的链应该贪心的尽量合并而不是拆开.则设f[i]为i子树内满足选的链尽量多的情况下根所在的链的最长长度即可.于是可以线性对某个k求得答案. 注意到长度为k的链不 ...