PAT解题报告A1111
来源:互联网 发布:徐州华道数据待遇好吗 编辑:程序博客网 时间:2024/05/20 02:30
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
Sample Input 1:10 150 1 0 1 18 0 0 1 14 8 1 1 13 4 0 3 23 9 1 4 10 6 0 1 17 5 1 2 18 5 1 2 12 3 0 2 22 1 1 1 11 3 0 3 11 4 0 1 19 7 1 3 15 1 0 5 26 5 1 1 23 5Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5Time = 3: 3 -> 1 -> 5Sample Input 2:
7 90 4 1 1 11 6 1 1 32 6 1 1 12 5 1 2 23 0 0 1 13 1 1 1 33 2 1 1 24 5 0 2 26 5 1 1 23 5Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
个人解题理解:
首先,这个问题比我想象的要复杂,因为涉及到时间和路径,所以需要执行两次不同的Djikstra算法来求最短路径/最快路径(即时间最短)。
其次,在记录路径的前驱节点的时候,需要用vector的数组,因为每个节点的前驱的个数是不定的。
最后,就是两个不同的DFS中,路径需要用静态局部变量来记录以保证递归调用中不被重复初始化。还需要审清楚题,距离最短路径有多条时选用时最短的一条;时间最快的路径有多条时选节点最少的一条。
#include<cstdio>#include<algorithm>#include<vector>#include<stack>using namespace std;const int maxv = 510;const int INF = 0x7FFFFFFF;int n, m;int Start, End;int Length[maxv][maxv];int Time[maxv][maxv];int d[maxv];bool vis[maxv];stack<int>PathA;stack<int> PathB;vector<int>lengthPre[maxv];vector<int>timePre[maxv];void Reset() {fill(d, d + maxv, INF);fill(vis, vis + maxv, false);}void DLength(int v) {d[v] = 0;int MinTime = INF;for (int i = 0;i < n;i++) {int u = -1, Min = INF;for (int j = 0;j < n;j++) {if (vis[j] == false && d[j] < Min) {Min = d[j];u = j;}}if (u == -1) return;vis[u] = true;for (int v = 0;v < n;v++) {if (vis[v] == false && Length[u][v] != INF) {if (d[u] + Length[u][v] < d[v]) {d[v] = d[u] + Length[u][v];lengthPre[v].clear();lengthPre[v].push_back(u);}if (d[u] + Length[u][v] == d[v]) {lengthPre[v].push_back(u);}}}}}int Dis=0;int MinTime = INF;void DFS(int e) {static int t = 0;static int d = 0;static stack<int >s;if (e == Start) {s.push(e);if (t < MinTime) {MinTime = t;PathA = s;Dis = d;}s.pop();}else {s.push(e);for (int i = 0;i < lengthPre[e].size();i++) {d += Length[lengthPre[e][i]][e];t = t + Time[lengthPre[e][i]][e];DFS(lengthPre[e][i]);t = t - Time[lengthPre[e][i]][e];d -= Length[lengthPre[e][i]][e];}s.pop();}}void DTime(int v) {d[v] = 0;for (int i = 0;i < n;i++) {int u = -1, Min = INF;for (int j = 0;j < n;j++) {if (vis[j] == false && d[j] < Min) {Min = d[j];u = j;}}if (u == -1) return;vis[u] = true;for (int v = 0;v < n;v++) {if (vis[v] == false && Time[u][v] != INF) {if (d[u] + Time[u][v] < d[v]) {d[v] = d[u] + Time[u][v];timePre[v].clear();timePre[v].push_back(u);}if (d[u] + Time[u][v] == d[v]) {timePre[v].push_back(u);}}}}}int T = 0;int MinNode = INF;void DFS2(int e) {static int n = 0;static int t = 0;static stack<int >s1;if (e == Start) {s1.push(e);if (n < MinNode) {MinNode = n;PathB = s1;T = t;}s1.pop();}else {s1.push(e);for (int i = 0;i < timePre[e].size();i++) {n++;t += Time[timePre[e][i]][e];DFS2(timePre[e][i]);t -= Time[timePre[e][i]][e];n--;}s1.pop();}}int main() {fill(Length[0], Length[0] + maxv*maxv, INF);fill(Time[0], Time[0] + maxv*maxv, INF);Reset();scanf("%d%d", &n, &m);int v1, v2, one, length, time;for (int i = 0;i < m;i++) {scanf("%d%d%d%d%d", &v1, &v2, &one, &length, &time);if (one == 1) {Length[v1][v2] = length;Time[v1][v2] = time;}else {Length[v1][v2] = Length[v2][v1] = length;Time[v1][v2] = Time[v2][v1] = time;}}scanf("%d%d", &Start, &End);DLength(Start);DFS(End);Reset();DTime(Start);DFS2(End);if (PathA != PathB) {printf("Distance = %d:", Dis);int len = PathA.size();for (int i = 0;i < len;i++) {if (i != len - 1)printf(" %d ->", PathA.top());else printf(" %d\n", PathA.top());PathA.pop();}printf("Time = %d:", T);len = PathB.size();for (int i = 0;i < len;i++) {if (i != len - 1)printf(" %d ->", PathB.top());else printf(" %d\n", PathB.top());PathB.pop();}}else {printf("Distance = %d; ", Dis);printf("Time = %d:", T);int len = PathB.size();for (int i = 0;i < len;i++) {if (i != len - 1)printf(" %d ->", PathB.top());else printf(" %d\n", PathB.top());PathB.pop();}}return 0;}
- PAT解题报告A1111
- PAT A1111
- PAT-A1111
- PAT解题报告索引
- PAT-1010解题报告
- PAT-1021解题报告
- PAT 解题报告
- pat解题报告【1076】
- pat解题报告【1073】
- pat解题报告【1078】
- pat解题报告【1082】
- pat解题报告【1074】
- PAT解题报告A1087
- PAT乙级解题报告目录
- 解题报告-PAT-List Leaves
- 解题报告-PAT-File Transfer
- PAT甲级解题报告汇总
- PAT (Top Level) 解题报告
- boost filesystem 教程
- C# CulPost,用于微信上传多媒体文件中的图片
- 在线考试系统(一)---JS倒计时cookie防刷新
- 【Javaweb】笔面试题 ---(1)
- Android 常用的网络框架
- PAT解题报告A1111
- spring中的AOP编程及代理模式
- div和span标签
- RDIFramework.NET平台代码生成器V3.2版本全新发布-更新于2017-02-27(提供下载-免费使用)
- tomcat使用
- vue-计算属性computed
- textarea 换行 (自动伸缩)|兼容ie6以上
- 请写出并说明如何在命令行下运行PHP脚本(写出两种方式)同时向PHP脚本传递参数?
- 小白笔记---------------------利用kali破解wifi