PAT解题报告A1111
2017-02-27 16:07
225 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
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 15 0 1 0 1 1 8 0 0 1 1 4 8 1 1 1 3 4 0 3 2 3 9 1 4 1 0 6 0 1 1 7 5 1 2 1 8 5 1 2 1 2 3 0 2 2 2 1 1 1 1 1 3 0 3 1 1 4 0 1 1 9 7 1 3 1 5 1 0 5 2 6 5 1 1 2 3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5 Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9 0 4 1 1 1 1 6 1 1 3 2 6 1 1 1 2 5 1 2 2 3 0 0 1 1 3 1 1 1 3 3 2 1 1 2 4 5 0 2 2 6 5 1 1 2 3 5
Sample 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 (Advanced Level) 1003. Emergency (25) 解题报告
- PAT (Advanced Level) Practise 1002 解题报告
- PAT (Advanced Level) 1038. Recover the Smallest Number (30) 解题报告
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30) 解题报告
- PAT (Basic Level) Practise(中文)----30题解题报告
- PAT (Advanced Level) Practise 1001 解题报告
- PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告
- PAT (Advanced Level) Practise 1003 解题报告
- [置顶] PAT甲级解题报告索引
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
- PAT (Top Level) 解题报告
- PAT (Advanced Level) Practise 1004 解题报告
- PAT解题报告A1087
- PAT 解题报告 1009. Product of Polynomials (25)
- PAT-乙级练习题-1058~1065-解题报告
- pat--1009product of polynomial解题报告
- pat basic 1001 解题报告样例
- 解题报告-PAT-File Transfer
- PAT1060解题报告
- PAT 基于词频的文件相似度 (set) -- 解题报告