PAT_A1111#Online Map
Source:
Description:
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), 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
andV2
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 fromV1
toV2
, or 0 if not;length
is the length of the street; andtime
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
Keys:
Attention:
- 其实就是求两次最短路径,代码code一遍再copy一遍,考试的时候怎么快怎么来,优化代码反而容易出错
Code:
1 /* 2 Data: 2019-08-18 21:30:18 3 Problem: PAT_A1111#Online Map 4 AC: 42:06 5 6 题目大意: 7 给出当前位置和目的地,给出最短路径和最快路径 8 输入: 9 第一行给出,结点数N,边数M 10 接下来M行,v1,v2,单/双向,距离,时间 11 最后一行,起点,终点 12 输出: 13 最短距离,输出路径,不唯一则选择最快的 14 最短时间,输出路径,不唯一则选择经过结点最少的 15 */ 16 #include<cstdio> 17 #include<vector> 18 #include<algorithm> 19 using namespace std; 20 const int M=1e3,INF=1e9; 21 int grap[M][M],cost[M][M],vis[M],d[M],c[M]; 22 int n,m,v1,v2,one,optTime=INF,optLeth=INF; 23 vector<int> preL[M],preT[M],optL,pathL,optT,pathT; 24 25 void DijskraL(int s) 26 { 27 fill(vis,vis+M,0); 28 fill(d,d+M,INF); 29 d[s]=0; 30 for(int i=0; i<n; i++) 31 { 32 int u=-1,Min=INF; 33 for(int j=0; j<n; j++) 34 { 35 if(vis[j]==0 && d[j]<Min) 36 { 37 Min = d[j]; 38 u = j; 39 } 40 } 41 if(u==-1) return; 42 vis[u]=1; 43 for(int v=0; v<n; v++) 44 { 45 if(vis[v]==0 && grap[u][v]!=INF) 46 { 47 if(d[u]+grap[u][v] < d[v]) 48 { 49 preL[v].clear(); 50 preL[v].push_back(u); 51 d[v] = d[u]+grap[u][v]; 52 } 53 else if(d[u]+grap[u][v]==d[v]) 54 preL[v].push_back(u); 55 } 56 } 57 } 58 } 59 60 void DFSL(int s) 61 { 62 if(s == v1) 63 { 64 pathL.push_back(v1); 65 int time=0,len=pathL.size(); 66 for(int i=1; i<len; i++) 67 time += cost[pathL[i]][pathL[i-1]]; 68 if(time < optTime) 69 { 70 optTime = time; 71 optL = pathL; 72 } 73 pathL.pop_back(); 74 return; 75 } 76 pathL.push_back(s); 77 for(int i=0; i<preL[s].size(); i++) 78 DFSL(preL[s][i]); 79 pathL.pop_back(); 80 } 81 82 void DijskraT(int s) 83 { 84 fill(vis,vis+M,0); 85 fill(c,c+M,INF); 86 c[s]=0; 87 for(int i=0; i<n; i++) 88 { 89 int u=-1,Min=INF; 90 for(int j=0; j<n; j++) 91 { 92 if(vis[j]==0 && c[j]<Min) 93 { 94 Min = c[j]; 95 u = j; 96 } 97 } 98 if(u==-1) return; 99 vis[u]=1; 100 for(int v=0; v<n; v++) 101 { 102 if(vis[v]==0 && cost[u][v]!=INF) 103 { 104 if(c[u]+cost[u][v] < c[v]) 105 { 106 preT[v].clear(); 107 preT[v].push_back(u); 108 c[v] = c[u] + cost[u][v]; 109 } 110 else if(c[u]+cost[u][v]==c[v]) 111 preT[v].push_back(u); 112 } 113 } 114 } 115 } 116 117 void DFST(int s) 118 { 119 if(s == v1) 120 { 121 pathT.push_back(v1); 122 if(pathT.size() < optLeth) 123 { 124 optLeth = pathT.size(); 125 optT = pathT; 126 } 127 pathT.pop_back(); 128 return; 129 } 130 pathT.push_back(s); 131 for(int i=0; i<preT[s].size(); i++) 132 DFST(preT[s][i]); 133 pathT.pop_back(); 134 } 135 136 int main() 137 { 138 #ifdef ONLINE_JUDGE 139 #else 140 freopen("Test.txt", "r", stdin); 141 #endif // ONLINE_JUDGE 142 143 fill(grap[0],grap[0]+M*M,INF); 144 fill(cost[0],cost[0]+M*M,INF); 145 146 scanf("%d%d", &n,&m); 147 for(int i=0; i<m; i++) 148 { 149 scanf("%d%d%d", &v1,&v2,&one); 150 scanf("%d%d", &grap[v1][v2],&cost[v1][v2]); 151 if(!one) 152 { 153 grap[v2][v1]=grap[v1][v2]; 154 cost[v2][v1]=cost[v1][v2]; 155 } 156 } 157 scanf("%d%d", &v1,&v2); 158 DijskraL(v1); 159 DFSL(v2); 160 DijskraT(v1); 161 DFST(v2); 162 if(optT == optL) 163 { 164 printf("Distance = %d; Time = %d: %d", d[v2],c[v2],optT[optT.size()-1]); 165 for(int i=optT.size()-2; i>=0; i--) 166 printf(" -> %d", optT[i]); 167 } 168 else 169 { 170 printf("Distance = %d: %d", d[v2], optL[optL.size()-1]); 171 for(int i=optL.size()-2; i>=0; i--) 172 printf(" -> %d", optL[i]); 173 printf("\nTime = %d: %d", c[v2], optT[optT.size()-1]); 174 for(int i=optT.size()-2; i>=0; i--) 175 printf(" -> %d", optT[i]); 176 } 177 178 return 0; 179 }