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 <iostream>
  2 #include <algorithm>
  3 #include <vector>
  4 using namespace std;
  5 const int maxn = 510, inf = 0x7fffffff;
  6 struct Arc{
  7     int v;
  8     int tim, len;
  9 }; 
 10 vector<Arc> arc[maxn];
 11 int N, M, dis[maxn], tim[maxn], pre[maxn], pathLen[maxn], S, T;
 12 vector<int> disPath, timPath, tempPath;
 13 
 14 void scan(){
 15     scanf("%d%d", &N, &M);
 16     for(int i = 0; i < M; ++ i){
 17         int v1, v2, oneWay;
 18         Arc t;
 19         scanf("%d%d%d%d%d", &v1, &v2, &oneWay, &t.len, &t.tim);
 20         t.v = v2;    arc[v1].push_back(t);
 21         if(!oneWay)    t.v = v1, arc[v2].push_back(t);
 22     }
 23     scanf("%d%d", &S, &T);
 24 }
 25 
 26 void dijkstra_dis(int s){
 27     bool vis[maxn];
 28     fill(vis, vis+maxn, false);
 29     fill(dis, dis+maxn, inf);
 30     fill(tim, tim+maxn, inf);
 31     dis[s] = tim[s] = 0;
 32     for(int i = 0; i < N; ++ i){
 33         int u = -1, minDis = inf;
 34         for(int j = 0; j < N; ++ j){
 35             if(!vis[j] && dis[j] < minDis){
 36                 minDis = dis[j];
 37                 u = j;
 38             }
 39         }
 40         if(u == -1)    return;
 41         vis[u] = true;
 42         for(int k = 0; k < arc[u].size(); ++ k){
 43             int v = arc[u][k].v;
 44             if(!vis[v]){
 45                 if(dis[u] + arc[u][k].len < dis[v]){
 46                     dis[v] = dis[u] + arc[u][k].len;
 47                     tim[v] = tim[u] + arc[u][k].tim;
 48                     pre[v] = u;
 49                 }else if(dis[u] + arc[u][k].len == dis[v] && tim[u] + arc[u][k].tim < tim[v]){
 50                     tim[v] = tim[u] + arc[u][k].tim;
 51                     pre[v] = u;
 52                 }
 53             }
 54         }
 55     }
 56 }
 57 
 58 void dijkstra_tim(int s){
 59     bool vis[maxn];
 60     fill(vis, vis+maxn, false);
 61     fill(tim, tim+maxn, inf);
 62     fill(pathLen, pathLen+maxn, inf);
 63     tim[s] = 0, pathLen[s] = 1;
 64     for(int i = 0; i < N; i ++){
 65         int u = -1, minTim  = inf;
 66         for(int j = 0; j < N; j ++){
 67             if(!vis[j] && tim[j] < minTim){
 68                 minTim = tim[j];
 69                 u = j;
 70             }
 71         }
 72         if(u == -1)    return;
 73         vis[u] = true;
 74         for(int k = 0; k < arc[u].size(); k ++){
 75             int v = arc[u][k].v;
 76             if(!vis[v]){
 77                 if(tim[u] + arc[u][k].tim < tim[v]){
 78                     tim[v] = tim[u] + arc[u][k].tim;
 79                     pre[v] = u;
 80                     pathLen[v] = pathLen[u] + 1; 
 81                 }else if(tim[u] + arc[u][k].tim == tim[v] && pathLen[u]+1 < pathLen[v]){
 82                     pre[v] = u;
 83                     pathLen[v] = pathLen[u] + 1;
 84                 }
 85             }
 86         }
 87     }
 88 }
 89 
 90 void dfs(int t){
 91     tempPath.push_back(t);
 92     if(pre[t] != -1){
 93         dfs(pre[t]);
 94     }
 95 }
 96 
 97 void printPath(vector<int> &path){
 98     for(int i = path.size()-1; i >= 0; -- i){
 99         printf("%d", path[i]);
100         if(i != 0)    printf(" -> ");
101     }
102     printf("\n");
103 }
104 
105 int main()
106 {
107     fill(pre, pre+maxn, -1);
108     scan();
109     dijkstra_dis(S);
110     dfs(T);
111     disPath = tempPath;
112     fill(pre, pre+maxn, -1);
113     dijkstra_tim(S);
114     tempPath.clear();//clear data 
115     dfs(T);
116     timPath = tempPath;
117     if(timPath == disPath){
118         printf("Distance = %d; Time = %d: ", dis[T], tim[T]);
119         printPath(disPath);
120     }else{
121         printf("Distance = %d: ", dis[T]);    printPath(disPath);
122         printf("Time = %d: ", tim[T]);    printPath(timPath);
123     }    
124     return 0;
125 }

 

Posted on 2017-02-28 16:05  小小旅行商  阅读(118)  评论(0编辑  收藏  举报