PAT 1111
2016-08-01 12:47
375 查看
1111. Online Map (30)
时间限制300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
分析:求两条最短路径,一条是距离最短,一条是时间最短,如果距离最短不唯一则去距离最短中时间最短的,如果时间最短不唯一则取路途节点数最少的
一开始以为距离最短中的时间最短是从时间最短路径中取,然后测试点3错误,重新设置时间变量来算时间,因为时间最短路径不一定是距离最短路径,而且路过的点数也不相
同,所以不能替代。
#include <iostream> #include <string.h> #include <cmath> #include <vector> using namespace std; const int INF=1<<30; int n,m; int length[501][501],tim[501][501]; int path1[501]={-1},path2[501]={-1}; int dist[501]={INF},tt[501]={INF},cost[501]={0}; void lengthdij(int sour); void timedij(int sour); int main() { cin>>n>>m; memset(path1,-1,sizeof(path1)); memset(path2,-1,sizeof(path2)); for(int i=0;i<m;i++) { int point1,point2; int oneway; cin>>point1; cin>>point2; cin>>oneway; cin>>length[point1][point2]; cin>>tim[point1][point2]; if(!oneway){ length[point2][point1]=length[point1][point2]; tim[point2][point1]=tim[point1][point2]; } } int sour,dest; cin>>sour>>dest; timedij(sour); lengthdij(sour); vector<int> v1,v2; for(int houji1=dest;houji1!=-1;houji1=path1[houji1]){ v1.insert(v1.begin(),houji1); } for(int houji2=dest;houji2!=-1;houji2=path2[houji2]){ v2.insert(v2.begin(),houji2); } if(v1!=v2) { cout<<"Distance = "<<dist[dest]<<":"; for(int i=0;i<v1.size();i++) { if(i!=v1.size()-1) { cout<<" "<<v1[i]<<" "<<"->"; } else cout<<" "<<v1[i]<<endl; } cout<<"Time = "<<tt[dest]<<":"; for(int i=0;i<v2.size();i++) { if(i!=v2.size()-1) cout<<" "<<v2[i]<<" "<<"->"; else cout<<" "<<v2[i]<<endl; } } else{ cout<<"Distance = "<<dist[dest]<<";"; cout<<" Time = "<<tt[dest]<<":"; for(int i=0;i<v1.size();i++) { if(i!=v1.size()-1) cout<<" "<<v1[i]<<" "<<"->"; else cout<<" "<<v1[i]<<endl; } } return 0; } void lengthdij(int sour) { for(int i=0;i<n;i++) dist[i]=INF; int v[501]={0}; for(int i=0;i<n;i++){ if(length[sour][i]) { dist[i]=length[sour][i]; cost[i]=tim[sour][i]; path1[i]=sour;} } dist[sour]=0; v[sour]=1; int mark; while(1){ mark=-1;int min=INF; for(int i=0;i<n;i++) if(!v[i]&&dist[i]<min) { min=dist[i]; mark=i; } if(min==INF) return; v[mark]=1; for(int i=0;i<n;i++) if(!v[i]&&length[mark][i]) if(dist[i]>dist[mark]+length[mark][i]) { dist[i]=dist[mark]+length[mark][i]; cost[i]=cost[mark]+tim[mark][i]; path1[i]=mark; } else if(dist[i]==dist[mark]+length[mark][i]) { if(cost[i]>cost[mark]+tim[mark][i]) { cost[i]=cost[mark]+tim[mark][i]; path1[i]=mark; } } } } void timedij(int sour) { for(int i=0;i<n;i++) tt[i]=INF; int v[501]={0}; int jiedianshu[501]={0}; for(int i=0;i<n;i++){ if(tim[sour][i]){ tt[i]=tim[sour][i];jiedianshu[i]++; path2[i]=sour;} } tt[sour]=0; v[sour]=1; int mark; while(1){ mark=-1;int min=INF; for(int i=0;i<n;i++) if(!v[i]&&tt[i]<min) { min=tt[i]; mark=i; } if(min==INF) return; v[mark]=1; for(int i=0;i<n;i++) if(!v[i]&&tim[mark][i]) if(tt[i]>tt[mark]+tim[mark][i]) { tt[i]=tt[mark]+tim[mark][i]; path2[i]=mark; jiedianshu[i]++; } else if(tt[i]==tt[mark]+tim[mark][i]) { if(jiedianshu[i]>jiedianshu[mark]+1) { jiedianshu[i]=jiedianshu[mark]+1; path2[i]=mark; } } } }
相关文章推荐
- PAT(A) - 1111. Online Map (30)
- PAT 1111 —— Online Map(Dijkstra单源最短路径)
- PAT甲级1111
- PAT (Advanced Level) Practise 1111 Online Map (30)
- Online Map(PAT甲级 1111)
- *浙大PAT甲级 1111
- PAT(A)1111 Online Map (30)(最短路)
- PAT (Advanced Level) Practise 1111 Online Map (30)
- PAT甲级1111 Online Map
- NAT/PAT 我晕了
- PAT (Advanced Level) 1009. Product of Polynomials (25) 多项式相乘
- PAT乙级.1040. 有几个PAT(25)
- PAT 甲级 1014. Waiting in Line
- 乙级 PAT 1028. 人口普查(20)
- NAT,PAT、OSPF的相关配置
- PAT (Basic Level) Practise (中文)1001. 害死人不偿命的(3n+1)猜想 (15) C语言
- PAT - 03-树2 List Leaves (25分)
- PAT--1041. Be Unique
- pat-(L1-025)
- PAT 注意事项、学习要点及其总结