1030. Travel Plan (30)【最短路+路径记忆】——PAT (Advanced Level) Practise
来源:互联网 发布:男生发型软件 编辑:程序博客网 时间:2024/06/05 19:00
题目信息
1030. Travel Plan (30)
时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
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
解题思路
dijkstra算法同时记录路径
AC代码
#include <cstdio>using namespace std;const int MAX = 0x1fffffff;int mp[505][505];int mpcost[505][505];int v[505], vis[505], path[505], cost[505];int n, m, s, d, a, b, dis, cst;void dij(){ vis[s] = true; for (int i = 0; i < n; ++i){ v[i] = mp[s][i]; cost[i] = mpcost[s][i]; path[i] = s; } while (true){ int tmin = MAX, tid = -1; for (int i = 0; i < n; ++i){ if (!vis[i] && tmin > v[i]){ tmin = v[i]; tid = i; } } if (tid != -1){ vis[tid] = true; for (int i = 0; i < n; ++i){ if (!vis[i]){ if (v[tid] + mp[i][tid] < v[i]){ v[i] = v[tid] + mp[i][tid]; path[i] = tid; cost[i] = cost[tid] + mpcost[i][tid]; }else if (v[tid] + mp[i][tid] == v[i] && cost[tid] + mpcost[i][tid] < cost[i]){ path[i] = tid; cost[i] = cost[tid] + mpcost[i][tid]; } } } }else{ break; } }}void out(int id){ if (id == s){ printf("%d", s); return; } out(path[id]); printf(" %d", id);}int main(){ scanf("%d%d%d%d", &n, &m, &s, &d); for (int i = 0; i < n; ++i){ v[i] = MAX; for (int j = 0; j < n; ++j){ mp[i][j] = mpcost[i][j] = (i == j)? 0:MAX; } } for (int i = 0; i < m; ++i){ scanf("%d%d%d%d", &a, &b, &dis, &cst); mp[a][b] = mp[b][a] = dis; mpcost[a][b] = mpcost[b][a] = cst; } dij(); out(d); printf(" %d %d\n", v[d], cost[d]); return 0;}
- 1030. Travel Plan (30)【最短路+路径记忆】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1030 Travel Plan (30)
- PAT (Advanced Level) Practise 1030 Travel Plan (30)
- PAT (Advanced Level) 1030. Travel Plan (30) Dijkstra最短路径
- 【PAT Advanced Level】1030. Travel Plan (30)
- 【PAT】【Advanced Level】1030. Travel Plan (30)
- 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise
- 1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise
- PAT Advanced Level 1030. Travel Plan (30)(Java and C++)
- PAT--1030. Travel Plan(最短路)
- PAT 1030. Travel Plan (30) (最短路)
- PAT (Advanced) 1030. Travel Plan (30)
- Pat(Advanced Level)Practice--1030(Travel Plan)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- 1022. Digital Library (30)——PAT (Advanced Level) Practise
- [PAT] (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- Spring、Spring MVC、Struts2、优缺点整理
- HP ProLiant BL460c Gen8 服务器部署手记
- 看图学网络 三 网络核心部分的数据交换(电路 报文 分组 交换)
- Bootstrap3 - Using glyphicon as background image in CSS
- 黑马程序员——方法.构造方法,抽象方法
- 1030. Travel Plan (30)【最短路+路径记忆】——PAT (Advanced Level) Practise
- 【Codeforces Round 334 (Div 2)D】【数论 置换群 典型数据特判 附带打表代码】Moodular Arithmetic 满足f(k x)=k f(x)的映射个数
- 【IOS网络】ATS的小总结
- zookeeper服务优化
- java 正则表达式浅析
- 学习笔记之正则表达式
- js定时器
- runtime是什么以及怎么用
- 线段树 理解,求解数组区间求和,求最小值