分值 : 30 难度 : 中等题 思路 : 两次不同的Dijkstra,细节把握好就行,属于基本功题 坑点 : 代码略有冗余,写的太丑了,但是一次AC还是很欣慰的。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
#include <iostream>using namespace std;#define Max_ 9999999999int length[501][501] ;int Time[501][501] ;int pre[501];int Flag = 0 ;int Q1[501] ; int Q1_cur = 0 ;int Q2[501] ; int Q2_cur = 0 ;void print(int s ,int b ){ if(s!=-1) { print(pre[s] , b); if(b == 0) Q1[Q1_cur++] = s; else Q2[Q2_cur++] = s ; //if(Flag++) // cout << " -> "; //cout << s ; }}int main() { int N , M ,S,D; cin >> N >> M ; int v1 ,v2 ,one_way ,leng,Tim ; for(int i = 0 ; i< N ; i++) for(int j = 0 ; j< N ; j++) { length[i][j] =Max_ ; Time[i][j] = Max_ ; } for(int i = 0 ; i< M ; i++) { cin >> v1 >> v2 >> one_way >>leng>>Tim ; length[v1][v2] =leng ; Time[v1][v2] = Tim ; if(one_way ==0) { length[v2][v1]= leng ; Time[v2][v1] =Tim; } } cin >> S>>D ; int dict[N] ; int time_cost[N]; int flag[N] ; /* * deal with the shortest path */ for(int i = 0 ; i <N ; i++) { dict[i] =Max_ ; time_cost[i]= Max_ ; pre[i]= -1 ; flag[i] = 0 ; } dict[S] = 0 ;time_cost[S] = 0 ; int count = 0 ; while(count < N) { int min=Max_ , index; for(int i = 0 ; i < N ; i++) //find low dict { if(flag[i]) continue ; if(dict[i] < min ) { min = dict[i]; index = i ; } } flag[index] = 1 ; for(int j = 0 ; j<N; j++) { if(flag[j]) continue; if(dict[j] > dict[index] + length[index][j] ) { dict[j] = dict[index] + length[index][j] ; time_cost[j] = time_cost[index] + Time[index][j] ; pre[j] = index ; } if(dict[j] == dict[index] + length[index][j]) { if(time_cost[j] > time_cost[index] + Time[index][j] ) { time_cost[j] = time_cost[index] + Time[index][j] ; pre[j] = index ; } } } count ++ ; } // for(int i = 0 ;i < N ; i++) // cout << dict[i] <<" "; print(D,0) ; //cout <<endl ; /* * deal with the fastest path */ int total[N ] ; Flag = 0 ; for(int i = 0 ; i <N ; i++) { total[i] = 0 ; time_cost[i]= Max_ ; pre[i]= -1 ; flag[i] = 0 ; } time_cost[S] = 0 ; count = 0 ; while(count < N) { int min=Max_ , index; for(int i = 0 ; i < N ; i++) //find low dict { if(flag[i]) continue ; if(time_cost[i] < min ) { min = time_cost[i]; index = i ; } } flag[index] = 1 ; for(int j = 0 ; j<N; j++) { if(flag[j]) continue; if(time_cost[j] > time_cost[index] + Time[index][j] ) { time_cost[j] = time_cost[index] + Time[index][j] ; total[j] =total[index] +1 ; pre[j] = index ; } if(time_cost[j] == time_cost[index] + Time[index][j]) { if(total[j] > total[index]+1 ) { total[j] = total[index] +1 ; pre[j] = index ; } } } count ++ ; } // for(int i = 0 ;i < N ; i++) // cout << dict[i] <<" "; print(D,2) ; bool T = true ; for(int i = 0 ; i< N; i++) { if(Q1[i] != Q2[i]) { T=false ; break ; } else { if(Q1[i] == D) break ; } } if(T) { cout <<"Distance = "<<dict[D]<<"; Time = "<<time_cost[D]<<": "; for(int i = 0 ; i< Q1_cur ; i++) { if(Flag++) cout <<" -> "; cout <<Q1[i] ; } } else { cout <<"Distance = "<<dict[D]<<": "; for(int i = 0 ; i< Q1_cur ; i++) { if(Flag++) cout <<" -> "; cout <<Q1[i] ; } cout <<endl ; Flag = 0 ; cout <<"Time = "<<time_cost[D]<<": "; for(int i = 0 ; i< Q2_cur ; i++) { if(Flag++) cout <<" -> "; cout <<Q2[i] ; } cout <<endl ; }}