Title:
This question requires you to realize an exclusive online map of the ladder race. After the team members enter their school location and venue location, the map should recommend two routes: one is the fastest route to reach; One is the shortest route. The topic ensures that there is at least one reachable route on the map for any query request.
Input format:
Input two positive integers N (2 ≤ N ≤ 500) and M given in the first line, which are the number of all marked locations and the number of roads connecting locations in the map respectively. Then m lines, each line gives the information of a road in the following format:
V1 V2 one-way length time
Where V1 and V2 are the numbers of the two ends of the road (from 0 to N-1); If the road is a one-way line from V1 to V2, one-way is 1, otherwise it is 0; Length is the length of the road; Time is the time required to pass through the road. Finally, a pair of start and end numbers are given.
Output format:
First, output the fastest arrival time T and the route represented by node number in the following format:
Time = T: starting point => Node 1 => ... => End
Then output the shortest distance D and the route represented by node number in the following format on the next line:
Distance = D: starting point => Node 1 => ... => End
If the fastest arrival route is not unique, output the shortest of several fastest routes, and ensure that this route is unique. If the route with the shortest distance is not unique, the one with the least number of output path nodes ensures that this route is unique.
If the two routes are identical, they are output in the following format:
Time = T; Distance = D: starting point => Node 1 => ... => End
Input example 1:
10 15 0 1 0 1 1 8 0 0 1 1 4 8 1 1 1 5 4 0 2 3 5 9 1 1 4 0 6 0 1 1 7 3 1 1 2 8 3 1 1 2 2 5 0 2 2 2 1 1 1 1 1 5 0 1 3 1 4 0 1 1 9 7 1 1 3 3 1 0 2 5 6 3 1 2 1 5 3
Output example 1:
Time = 6: 5 => 4 => 8 => 3 Distance = 3: 5 => 1 => 3
Input example 2:
7 9 0 4 1 1 1 1 6 1 3 1 2 6 1 1 1 2 5 1 2 2 3 0 0 1 1 3 1 1 3 1 3 2 1 2 1 4 5 0 2 2 6 5 1 2 1 3 5
Output example 2:
Time = 3; Distance = 4: 3 => 2 => 5
Idea:
Dijkstra calculates the shortest circuit twice, and records the first node in the process of calculating
code:
#include<bits/stdc++.h> using namespace std; int n,m; int be,en; //Storage distance and time int s[505][505],tim[505][505]; //Did you join int vis[505]; //First node, total distance, total time int prel[505],pret[505],suml[505],sumt[505]; int anst[505],ansd[505]; const int inf=999999999; //Looking for the shortest time void dijkstraT() { vis[be]=1; int dis[505]; for(int i=0; i<n; i++) { dis[i]=inf; dis[be]=0; sumt[be]=0; for(int i=0; i<n; i++) { int misign=inf; int signid=be; for(int i=0; i<n; i++) { if(sumt[i]<misign&&vis[i]==0) { misign=sumt[i]; signid=i; } } vis[signid]=1; for(int i=0; i<n; i++) { // If the time is shorter if(sumt[i]>sumt[signid]+tim[signid][i]) { pret[i]=signid; sumt[i]=sumt[signid]+tim[signid][i]; dis[i]=dis[signid]+s[signid][i]; } // If the time is the same, but the distance is shorter else if(sumt[i]==sumt[signid]+tim[signid][i]&&dis[i]>dis[signid]+s[signid][i]) { pret[i]=signid; sumt[i]=sumt[signid]+tim[signid][i]; dis[i]=dis[signid]+s[signid][i]; } } } } } //Find the shortest distance void dijkstraD(){ int node[505]; for(int i=0;i<n;i++){ node[i]=0; } memset(vis,0,sizeof(vis)); vis[be]=1; suml[be]=0; for(int i=0;i<n;i++){ int signid=be; int misign=inf; for(int i=0;i<n;i++){ if(!vis[i]&&suml[i]<misign){ misign=suml[i]; signid=i; } } vis[signid]=1; for(int i=0;i<n;i++){ // If the distance is shorter if(suml[i]>suml[signid]+s[signid][i]){ prel[i]=signid; suml[i]=suml[signid]+s[signid][i]; node[i]=node[signid]+1; } // If the distance is the same, but there are fewer nodes else if(suml[i]==suml[signid]+s[signid][i]&&node[i]>node[signid]+1){ prel[i]=signid; suml[i]=suml[signid]+s[signid][i]; node[i]=node[signid]+1; } } } } int main() { cin>>n>>m; // initialization for(int i=0; i<n; i++) { prel[i]=-1; pret[i]=-1; suml[i]=inf; sumt[i]=inf; anst[i]=-1;ansd[i]=-1; vis[i]=0; for(int j=0; j<n; j++) { if(i==j) { s[i][j]=0; tim[i][j]=0; } else { s[i][j]=inf; tim[i][j]=inf; } } } // Input and convert to collar matrix for(int i=1; i<=m; i++) { int a,b,x,l,t; cin>>a>>b>>x>>l>>t; if(x==1) { s[a][b]=l; tim[a][b]=t; } else { s[a][b]=l; tim[a][b]=t; s[b][a]=l; tim[b][a]=t; } } cin>>be>>en; dijkstraT(); dijkstraD(); // Judge whether the two nodes are the same int pr=en,id1=0,id2=0,flag=0; while(pr!=-1) { anst[id1]=pr; id1++; pr=pret[pr]; } pr=en; while(pr!=-1) { ansd[id2]=pr; if(anst[id2]!=pr){ flag=-1; } id2++; pr=prel[pr]; } // Two nodes are the same if(id1==id2&&flag==0){ cout<<"Time = "<<sumt[en]<<"; Distance = "<<suml[en]<<": "; for(int i=id2-1;i>=0;i--){ cout<<ansd[i]; if(i!=0){ cout<<" => "; } } } // The two nodes are different else{ cout<<"Time = "<<sumt[en]<<": "; for(int i=id1-1;i>=0;i--){ cout<<anst[i]; if(i!=0){ cout<<" => "; } } cout<<endl; cout<<"Distance = "<<suml[en]<<": "; for(int i=id2-1;i>=0;i--){ cout<<ansd[i]; if(i!=0){ cout<<" => "; } } } return 0; }