1111 Online Map (30 分)

1111 Online Map (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 (2N500), 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 N1) 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

分析:Dijkstra模板题,套2次就行了。

  1 /**
  2 * Copyright(c)
  3 * All rights reserved.
  4 * Author : Mered1th
  5 * Date : 2019-02-26-22.31.18
  6 * Description : A1111
  7 */
  8 #include<cstdio>
  9 #include<cstring>
 10 #include<iostream>
 11 #include<cmath>
 12 #include<algorithm>
 13 #include<string>
 14 #include<unordered_set>
 15 #include<map>
 16 #include<vector>
 17 #include<set>
 18 using namespace std;
 19 const int maxn=510;
 20 const int INF=1000000000;
 21 bool vis[maxn];
 22 int G[maxn][maxn],d[maxn],t[maxn],T[maxn][maxn];
 23 int n,m,st,ed;
 24 int pred[maxn],pret[maxn],mininter[maxn];
 25 vector<int> shortPath,fastPath;
 26 void dDijkstra(int s){
 27     fill(d,d+maxn,INF);
 28     fill(t,t+maxn,INF);
 29     d[s]=0;
 30     t[s]=0;
 31     memset(vis,0,sizeof(vis));
 32     for(int i=0;i<n;i++) pred[i]=i;
 33     for(int i=0;i<n;i++){
 34         int u=-1,MIN=INF;
 35         for(int j=0;j<n;j++){
 36             if(vis[j]==false && d[j]<MIN){
 37                 u=j;
 38                 MIN=d[j];
 39             }
 40         }
 41         if(u==-1) return;
 42         vis[u]=true;
 43         for(int v=0;v<n;v++){
 44             if(vis[v]==false && G[u][v]!=INF){
 45                 if(G[u][v]+d[u]<d[v]){
 46                     d[v]=d[u]+G[u][v];
 47                     t[v]=t[u]+T[u][v];
 48                     pred[v]=u;
 49                 }
 50                 else if(G[u][v]+d[u]==d[v]){
 51                     if(T[u][v]+t[u]<t[v]){
 52                         pred[v]=u;
 53                         t[v]=T[u][v]+t[u];
 54                     }
 55                 }
 56             }
 57         }
 58     }
 59     for(int i=ed;i!=st;i=pred[i]){
 60         shortPath.push_back(i);
 61     }
 62     shortPath.push_back(st);
 63 }
 64 
 65 void tDijkstra(int s){
 66     fill(t,t+maxn,INF);
 67     t[s]=0;
 68     fill(mininter,mininter+maxn,1);
 69     memset(vis,0,sizeof(vis));
 70     for(int i=0;i<n;i++) pret[i]=i;
 71     for(int i=0;i<n;i++){
 72         int u=-1,MIN=INF;
 73         for(int j=0;j<n;j++){
 74             if(vis[j]==false && t[j]<MIN){
 75                 u=j;
 76                 MIN=t[j];
 77             }
 78         }
 79         if(u==-1) return;
 80         vis[u]=true;
 81         for(int v=0;v<n;v++){
 82             if(vis[v]==false && T[u][v]!=INF){
 83                 if(T[u][v]+t[u]<t[v]){
 84                     t[v]=t[u]+T[u][v];
 85                     pret[v]=u;
 86                     mininter[v]=mininter[u]+1;
 87                 }
 88                 else if(T[u][v]+t[u]==t[v]){
 89                     if(mininter[v]>mininter[u]+1){
 90                         pret[v]=u;
 91                         mininter[v]=mininter[u]+1;
 92                     }
 93                 }
 94             }
 95         }
 96     }
 97     for(int i=ed;i!=st;i=pret[i]){
 98         fastPath.push_back(i);
 99     }
100     fastPath.push_back(st);
101 }
102 
103 
104 int main(){
105 #ifdef ONLINE_JUDGE
106 #else
107     freopen("1.txt", "r", stdin);
108 #endif
109     int u,v,o;
110     cin>>n>>m;
111     fill(G[0],G[0]+maxn*maxn,INF);
112     fill(T[0],T[0]+maxn*maxn,INF);
113     for(int i=0;i<m;i++){
114         cin>>u>>v>>o;
115         cin>>G[u][v]>>T[u][v];
116         if(o==0){
117             G[v][u]=G[u][v];
118             T[v][u]=T[u][v];
119         }
120     }
121     cin>>st>>ed;
122     dDijkstra(st);
123     tDijkstra(st);
124     if(shortPath!=fastPath){
125         printf("Distance = %d: ",d[ed]);
126         for(int i=shortPath.size()-1;i>=0;i--){
127             printf("%d",shortPath[i]);
128             if(i!=0) printf(" -> ");
129             else printf("\n");
130         }
131         printf("Time = %d: ",t[ed]);
132         for(int i=fastPath.size()-1;i>=0;i--){
133             printf("%d",fastPath[i]);
134             if(i!=0) printf(" -> ");
135             else printf("\n");
136         }
137     }
138     else{
139         printf("Distance = %d; Time = %d: ",d[ed],t[ed]);
140         for(int i=fastPath.size()-1;i>=0;i--){
141             printf("%d",fastPath[i]);
142             if(i!=0) printf(" -> ");
143             else printf("\n");
144         }
145     }
146     return 0;
147 }

猜你喜欢

转载自www.cnblogs.com/Mered1th/p/10441192.html