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 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 題目分析
就是用求最短路徑,不過用了兩次而已,思路很清晰,代碼比較長 第一次用dfs做,最後一個測試點逾時 27分 現在用dijkstra做,測試點2沒過 23分(也是醉了) 不過這道題,每做的一次就回讓我對dijkstra有一次比較深的理解 還在測試點2還沒解決 維基百科上的dijkstra演算法
1 function Dijkstra(Graph, source):
2
3 create vertex set Q
4
5 for each vertex v in Graph: // Initialization
6 dist[v] ← INFINITY // Unknown distance from source to v
7 prev[v] ← UNDEFINED // Previous node in optimal path from source
8 add v to Q // All nodes initially in Q (unvisited nodes)
9
10 dist[source] ← 0 // Distance from source to source
11
12 while Q is not empty:
13 u ← vertex in Q with min dist[u] // Source node will be selected first
14 remove u from Q
15
16 for each neighbor v of u: // where v is still in Q.
17 alt ← dist[u] + length(u, v)
18 if alt < dist[v]: // A shorter path to v has been found
19 dist[v] ← alt
20 prev[v] ← u
21
22 return dist[], prev[]
code
#include<iostream>#include<climits>#include<stack>#define inf INT_MAXusing namespace std;int a[510][510];int b[510][510];//求最短路徑是選擇 參數 a b //最短時間 參數 b a//前結點儲存在 a[i][i]中int sp[510];//記錄當前最短int st[510];//記錄當前最短void dijkstra(int(*a)[510],int (*b)[510],int start,int end,int n){ bool visit[510]; //初始化,前序 for(int i=0;i<n;i++) { visit[i]=false; sp[i]=inf; st[i]=inf; } a[start][start]=start; sp[start]=0; st[start]=0; visit[start]=true; int begin=start; while(true) { //更新 for(int i=0;i<n;i++) { if(a[start][i]==-1) continue; if(sp[i]>a[start][i]+sp[start]) { sp[i]=a[start][i]+sp[start]; st[i]=b[start][i]+st[start]; //start=i;//這樣不全 a[i][i]=start; }else if(sp[i]==a[start][i]+sp[start]) { if(st[i]>b[start][i]+st[start]) { st[i]=b[start][i]+st[start]; a[i][i]=start; } } } int min=inf; int begin=-1; //選擇起始點,必須從全域找,這樣的貪心才是最 for(int i=0;i<n;i++) { if(!visit[i]&&sp[i]<min) { min=sp[i]; begin=i; } } if(begin==-1) break; start=begin; visit[start]=true; if(start==end) break; }}//dijstra演算法int main(){ for(int i=0;i<510;i++) { for(int j=0;j<510;j++) { a[i][j]=-1; b[i][j]=-1; } } int N,M; cin>>N>>M; int v1,v2,one_way,length,_time; for(int i=0;i<M;i++) { cin>>v1>>v2>>one_way; cin>>a[v1][v2]; cin>>b[v1][v2]; if(one_way==0) { a[v2][v1]=a[v1][v2]; b[v2][v1]=b[v1][v2]; } } int s,d; cin>>s>>d; bool isSame=true; stack<int> st1,st2; int Dis,Time; dijkstra(a,b,s,d,N); Dis=sp[d]; int t=d; st1.push(d); while(true) { if(a[t][t]==t) break; st1.push(a[t][t]); t=a[t][t]; } dijkstra(b,a,s,d,N); Time=sp[d]; t=d; st2.push(d); while(true) { if(b[t][t]==t) break; if(b[t][t]!=a[t][t]) isSame=false; st2.push(b[t][t]); t=b[t][t]; } if(isSame) { cout<<"Distance = "<<Dis<<"; Time = "<<Time<<": "; while(st1.empty()==false) { if(st1.top()==d) cout<<st1.top()<<endl; else cout<<st1.top()<<" -> "; st1.pop(); } }else { cout<<"Distance = "<<Dis<<": "; while(st1.empty()==false) { if(st1.top()==d) cout<<st1.top()<<endl; else cout<<st1.top()<<" -> "; st1.pop(); } cout<<"Time = "<<Time<<": "; while(st2.empty()==false) { if(st2.top()==d) cout<<st2.top()<<endl; else cout<<st2.top()<<" -> "; st2.pop(); } } return 0;}