A1111. Online Map

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 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

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<vector>
  5 using namespace std;
  6 const int INF = 100000000;
  7 int GL[501][501], GT[501][501];
  8 int dst[501], visit[501];
  9 vector<int> pre1[501], pre2[501];
 10 int N, M, sour, destn;
 11 void dijkstra(int G[501][501], vector<int> pre[], int s){
 12     fill(dst, dst + 501, INF);
 13     fill(visit, visit + 501, 0);
 14     dst[s] = 0;
 15     for(int i = 0; i < N; i++){
 16         int u = -1, minLen = INF;
 17         for(int j = 0; j < N; j++){
 18             if(dst[j] < minLen && visit[j] == 0){
 19                 minLen = dst[j];
 20                 u = j;
 21             }
 22         }
 23         if(u == -1){
 24             return;
 25         }
 26         visit[u] = 1;
 27         for(int j = 0; j < N; j++){
 28             if(visit[j] == 0 && G[u][j] != INF){
 29                 if(G[u][j] + dst[u] < dst[j]){
 30                     dst[j] = G[u][j] + dst[u];
 31                     pre[j].clear();
 32                     pre[j].push_back(u);
 33                 }else if(G[u][j] + dst[u] == dst[j]){
 34                     pre[j].push_back(u);
 35                 }
 36             }
 37         }
 38     }
 39 }
 40 int minTime = INF, minLen = INF;
 41 vector<int> ans1, ans2, temp1, temp2;
 42 void DFS1(int d){ //函数前初始化minTime、minLen
 43     temp1.push_back(d);
 44     if(d == sour){
 45         int tempL = 0, tempT = 0;
 46         for(int i = temp1.size() - 1; i > 0; i--){
 47             tempL += GL[temp1[i]][temp1[i - 1]];
 48             tempT += GT[temp1[i]][temp1[i - 1]];
 49         }
 50         if(tempL < minLen){
 51             minLen = tempL;
 52             minTime = tempT;
 53             ans1 = temp1;
 54         }else if(tempL == minLen && tempT < minTime){
 55             minLen = tempL;
 56             minTime = tempT;
 57             ans1 = temp1;
 58         }
 59     }
 60 
 61     for(int i = 0; i < pre1[d].size(); i++){
 62         DFS1(pre1[d][i]);
 63     }
 64     temp1.pop_back();
 65 }
 66 void DFS2(int d){
 67     temp2.push_back(d);
 68     if(d == sour){
 69         int tempL = 0, tempT = 0;
 70         for(int i = temp2.size() - 1; i > 0; i--){
 71             tempL += GL[temp2[i]][temp2[i - 1]];
 72             tempT += GT[temp2[i]][temp2[i - 1]];
 73         }
 74         if(tempT < minTime){
 75             ans2 = temp2;
 76             minTime = tempT;
 77             minLen = tempL;
 78         }else if(tempT == minTime && temp2.size() < ans2.size()){
 79             ans2 = temp2;
 80             minTime = tempT;
 81             minLen = tempL;
 82         }
 83     }
 84     for(int i = 0; i < pre2[d].size(); i++){
 85         DFS2(pre2[d][i]);
 86     }
 87     temp2.pop_back();
 88 }
 89 int main(){
 90     scanf("%d%d", &N, &M);
 91     fill(GL[0], GL[0] + 501*501, INF);
 92     fill(GT[0], GT[0] + 501*501, INF);
 93     for(int i = 0; i < M; i++){
 94         int tag, v1, v2, L, T;
 95         scanf("%d%d%d%d%d", &v1, &v2, &tag, &L, &T);
 96         if(tag == 1){
 97             GL[v1][v2] = L;
 98             GT[v1][v2] = T;
 99         }else{
100             GL[v1][v2] = GL[v2][v1] = L;
101             GT[v1][v2] = GT[v2][v1] = T;    
102         }
103     }
104     scanf("%d%d", &sour, &destn);
105     dijkstra(GL, pre1, sour);
106     dijkstra(GT, pre2, sour);
107     minTime = INF; minLen = INF;
108     DFS1(destn);
109     int prtLen = minLen;
110     minTime = INF; minLen = INF;
111     DFS2(destn);
112     int prtTim = minTime;
113     if(ans1 == ans2){
114         int LL = ans1.size();
115         printf("Distance = %d; Time = %d: %d", prtLen, prtTim, ans1[LL - 1]);
116         for(int i = LL - 2; i >= 0; i--){
117             printf(" -> %d", ans1[i]);
118         }
119     }else{
120         int LL1 = ans1.size(), LL2 = ans2.size();
121         printf("Distance = %d: %d", prtLen, ans1[LL1 - 1]);
122         for(int i = LL1 - 2; i >= 0; i--){
123             printf(" -> %d", ans1[i]);
124         }
125         printf("\n");
126         printf("Time = %d: %d", prtTim, ans2[LL2 - 1]);
127         for(int i = LL2 - 2; i >= 0; i--){
128             printf(" -> %d", ans2[i]);
129         }
130     }
131     cin >> N;
132     return 0;
133 }
View Code

总结:

1、题意:用路程作为边权,求最短路径,如果有多条则输出耗时最短的。 再用时间作为边权求最短路,如果有多条则输出经过节点个数最少的。使用两次迪杰斯特拉和DFS即可。最开始没读清题,以为以时间为边权求最短路,如果有多条则选择路程最短的,结果测试点2过不去。

2、注意one-way是单行道标志,为1表示只有v1到v2的路,为0表示v1到v2和v2到v1都是通的。注意有些是单向路径,有些是双向。另外,由于DFS回溯时得到的路径是倒着的、有些路是单向的,所以在累加边权时也是倒序的,是 GL[temp1[i]][temp1[i - 1]]  而非 GL[temp1[i - 1]][temp1[ i ]]。

posted @ 2018-03-03 16:14  ZHUQW  阅读(263)  评论(0编辑  收藏  举报