1111. Online Map (30)
来源:互联网 发布:mac可以玩跑跑卡丁车吗 编辑:程序博客网 时间:2024/06/11 19:09
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
#include<iostream>#include<queue>#include<vector>#include<cstdio>using namespace std;typedef pair<int ,int > P;int map_length[505][505];int map_time[505][505];int length[505],time1[505];int vis_length[505],vis_time[505];int path_length[505],path_time[505];int path1[505],path2[505],cro[505];int n,m;int start,endx;const int INF = 1<<29;void dij_length(int s){ for (int i=0;i<=n;i++) { vis_length[i]=0; length[i]=INF; } length[s]=0; priority_queue< P,vector<P>,greater<P> > que; que.push({length[s],s}); while(que.size()) { P p=que.top(); que.pop(); int num=p.second; if (vis_length[num]) continue; vis_length[num]=1; for (int i=0;i<n;i++) { if (map_length[num][i]!=INF) { if (length[i]>length[num]+map_length[num][i]) { length[i]=length[num]+map_length[num][i]; time1[i]=time1[num]+map_time[num][i]; path_length[i]=num; que.push({length[i],i}); } else if(length[i]==length[num]+map_length[num][i]) { if (time1[i]>time1[num]+map_time[num][i]) { path_length[i]=num; time1[s]=time1[num]+map_time[num][i]; } } } } }}void dij_time(int s){ for (int i=0;i<=n;i++) { vis_time[i]=0; time1[i]=INF; cro[i]=0; } time1[s]=0; cro[s]=1; priority_queue< P,vector<P>,greater<P> > que; que.push({time1[s],s}); while(que.size()) { P p=que.top(); que.pop(); int num=p.second; if (vis_time[num]) continue; vis_time[num]=1; for (int i=0;i<n;i++) { if (map_time[num][i]!=INF) { if (time1[i]>time1[num]+map_time[num][i]) { time1[i]=time1[num]+map_time[num][i]; path_time[i]=num; cro[i]=cro[num]+1; que.push({time1[i],i}); } else if(time1[i]==time1[num]+map_time[num][i]) { if (cro[i]>cro[num]+1) { path_time[i]=num; cro[i]=cro[num]+1; } } } } }}int main(){ cin>>n>>m; int v1,v2,o_way,len,tim; for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) map_length[i][j]=map_time[i][j]=INF; for (int i=0;i<m;i++) { cin>>v1>>v2>>o_way>>len>>tim; if(o_way==1) { map_length[v1][v2]=len; map_time[v1][v2]=tim; } else { map_length[v1][v2]=map_length[v2][v1]=len; map_time[v1][v2]=map_time[v2][v1]=tim; } } cin>>start>>endx; dij_length(start); dij_time(start); int temp=endx,index1=0; while(path_length[temp]!=start) { path1[index1++]=temp; temp=path_length[temp]; } path1[index1++]=temp; int temp2=endx,index2=0; while(path_time[temp2]!=start) { path2[index2++]=temp2; temp2=path_time[temp2]; } path2[index2++]=temp2; if (index1==index2) { int flag=1; for (int i=0;i<index1;i++) { if (path1[i]!=path2[i]) { flag=0; break; } } if (flag==1) { printf("Distance = %d; Time = %d: %d",length[endx],time1[endx],start); for (int i=index1-1;i>=0;i--) printf(" -> %d",path1[i]); return 0; } } printf("Distance = %d: %d",length[endx],start); for (int i=index1-1;i>=0;i--) printf(" -> %d",path1[i]); printf("\nTime = %d: %d",time1[endx],start); for (int i=index2-1;i>=0;i--) printf(" -> %d",path2[i]); return 0;}
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- 1111. Online Map (30)
- PAT 1111. Online Map (30)
- pat 1111. Online Map (30)
- 1111. Online Map (30) <Dijkstra>
- solution Of Pat 1111. Online Map (30)
- 1111. Online Map (30)解题报告
- 1111. Online Map (30)[dijkstra算法]
- PAT(Advanced Level)1111. Online Map (30)
- 【PAT】【Advanced Level】1111. Online Map (30)
- PAT 1111. Online Map (30) Dijkstra
- tomcat安装与配置
- POJ 2528 Mayor's posters(加分割点)
- Android Webview 用 WebBackForwardList 来判断是否是返回的界面
- 1030. 完美数列(25)
- HDU 1569 find the safest road
- 1111. Online Map (30)
- UVA11584PartitioningByPalindromes
- Lazarus控件中控件尺寸变化的处理机制
- spring framework入门
- c语言笔记-printf()格式化输出详解
- Python爬内蒙古大学教务处并自动计算绩点(小窗体版)
- Linux下通过yum安装软件
- 链表与顺序表的区别
- deepin安装jetbrain系列破解+究竟装到哪里了