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 (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 5Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5 Time = 3: 3 -> 1 -> 5Sample 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 5Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
开始用的dfs,最后的点超时了,想想确实图用dfs,点多的话不合适,改用dijkstra,想在原来上改,结果改乱了,心态有点炸。
休息了,今天清醒了改一次就成了。没有强行为了统一用1个dijkstra函数,两种情况分别用。
贴两个,前面是dijkstra全过的,后面是27分的dfs。以后图还是用dijkstra,图练的太少。
#include<iostream> #include<vector> #include<algorithm> using namespace std; const int INF=0x1fffffff; int dis[500][500]; int tim[500][500]; bool v[500]; int n,m,s,e; int maxd,maxt; vector<int>answ; vector<int>anst; int d[500]; int td[500]; int last[500]; int cnt[500]; void dijkstra1(int dis[500][500],int tim[500][500]) { for(int i=0;i<n;i++) { last[i]=s; d[i]=dis[s][i]; td[i]=tim[s][i]; } d[s]=0;td[s]=0;v[s]=true; int start=s; while(start!=e) { int minnum; int min=INF,mint=INF; for(int i=0;i<n;i++) if(!v[i]&&d[i]<min) { min=d[i]; minnum=i; } v[minnum]=true; for(int i=0;i<n;i++) { if(!v[i]&&dis[minnum][i]+d[minnum]<d[i]) { d[i]=dis[minnum][i]+d[minnum]; td[i]=tim[minnum][i]+td[minnum]; last[i]=minnum; } else if(!v[i]&&dis[minnum][i]+d[minnum]==d[i]&&td[minnum]+tim[minnum][i]<td[i]) { td[i]=tim[minnum][i]+td[minnum]; last[i]=minnum; } } start=minnum; } } void dijkstra2(int tim[500][500],int cnt[500]) { for(int i=0;i<n;i++) { last[i]=s; v[i]=false; td[i]=tim[s][i]; if(tim[s][i]==INF) cnt[i]=INF; else cnt[i]=1; } td[s]=0;cnt[s]=0;v[s]=true; int start=s; while(start!=e) { int minnum; int min=INF,mint=INF; for(int i=0;i<n;i++) if(!v[i]&&td[i]<min) { min=td[i]; minnum=i; } v[minnum]=true; for(int i=0;i<n;i++) { if(!v[i]&&tim[minnum][i]+td[minnum]<td[i]) { td[i]=tim[minnum][i]+td[minnum]; cnt[i]=cnt[minnum]+1; last[i]=minnum; } else if(!v[i]&&tim[minnum][i]+td[minnum]==td[i]&&cnt[minnum]+1<cnt[i]) { cnt[i]=cnt[minnum]+1; last[i]=minnum; } } start=minnum; } } int main() { cin>>n>>m; for(int i=0;i<500;i++) { v[i]=false; for(int j=0;j<500;j++) { dis[i][j]=INF; tim[i][j]=INF; } } for(int i=0;i<m;i++) { bool ow; int l,t; cin>>s>>e>>ow>>l>>t; dis[s][e]=l; tim[s][e]=t; if(!ow) { dis[e][s]=l; tim[e][s]=t; } } cin>>s>>e; dijkstra1(dis,tim); maxd=d[e]; for(int i=e;last[i]!=i;) { answ.push_back(i); i=last[i]; } answ.push_back(s); reverse(answ.begin(),answ.end()); dijkstra2(tim,cnt); maxt=td[e]; for(int i=e;last[i]!=i;) { anst.push_back(i); i=last[i]; } anst.push_back(s); reverse(anst.begin(),anst.end()); if(answ==anst) { printf("Distance = %d; Time = %d: ",maxd,maxt); cout<<answ[0]; for(int i=1;i<answ.size();i++) cout<<" -> "<<answ[i]; cout<<endl; } else { printf("Distance = %d: ",maxd); cout<<answ[0]; for(int i=1;i<answ.size();i++) cout<<" -> "<<answ[i]; cout<<endl; printf("Time = %d: ",maxt); cout<<anst[0]; for(int i=1;i<anst.size();i++) cout<<" -> "<<anst[i]; cout<<endl; } return 0; }
<pre name="code" class="cpp">#include<iostream> #include<vector> using namespace std; const int INF=0x1fffffff; int dis[500][500]; int tim[500][500]; bool v[500]; int n,m,s,e; int maxd=INF,maxt=INF,cnt=INF; int maxdt=INF; vector<int>tmpw; vector<int>tmpt; vector<int>answ; vector<int>anst; void dfs(int start,int sumd,int sumt,int count) { if(start==e) { tmpw.push_back(start); tmpt.push_back(start); if(answ.empty()) { answ=tmpw; anst=tmpt; maxd=sumd,maxt=sumt,maxdt=sumt; cnt=count; } else { if((sumd<maxd)||(sumd==maxd&&sumt<maxdt)) { answ=tmpw; maxd=sumd; maxdt=sumt; } if(sumt<maxt||sumt==maxt&&count<cnt) { anst=tmpt; maxt=sumt; cnt=count; } } tmpw.pop_back(); tmpt.pop_back(); return ; } tmpw.push_back(start); tmpt.push_back(start); for(int i=0;i<500;i++) { if(dis[start][i]!=INF&&!v[i]) { v[start]=true; dfs(i,sumd+dis[start][i],sumt+tim[start][i],count+1); v[start]=false; } } tmpw.pop_back(); tmpt.pop_back(); } int main() { cin>>n>>m; for(int i=0;i<500;i++) { v[i]=false; for(int j=0;j<500;j++) { dis[i][j]=INF; tim[i][j]=INF; } } for(int i=0;i<m;i++) { bool ow; int l,t; cin>>s>>e>>ow>>l>>t; dis[s][e]=l; tim[s][e]=t; if(!ow) { dis[e][s]=l; tim[e][s]=t; } } cin>>s>>e; v[s]=true; dfs(s,0,0,0); if(answ==anst) { printf("Distance = %d; Time = %d: ",maxd,maxt); cout<<answ[0]; for(int i=1;i<answ.size();i++) cout<<" -> "<<answ[i]; cout<<endl; } else { printf("Distance = %d: ",maxd); cout<<answ[0]; for(int i=1;i<answ.size();i++) cout<<" -> "<<answ[i]; cout<<endl; printf("Time = %d: ",maxt); cout<<anst[0]; for(int i=1;i<anst.size();i++) cout<<" -> "<<anst[i]; cout<<endl; } return 0; }