At first I wrote this question with DFS = = result time overrun
For this shortest path problem, the higher efficiency is certainly the Floyd algorithm and the Dijkstra algorithm
Single source shortest path is best naturally with Dijkstra, but Floyd is easy to write, I wrote it.
#include <iostream>#defineINF 99999using namespacestd;intN, M, S, D;intmap[ -][ -];intprice[ -][ -];intMain () {/*the purity of the initialization! */Cin>> N >> m >> s >>D; for(inti =0; I < -; i++){ for(intj =0; J < -; J + +) {Map[i][j]= Price[i][j] =INF; } } for(inti =0; I < m; i++){ intW, x, Y, Z; CIN>> x >> y >> z >>W; Map[x][y]= Map[y][x] =Z; Price[x][y]= Price[y][x] =W; } //Floyd Algorithm for(intK =0; K < n; k++){ for(inti =0; I < n; i++){ for(intj =0; J < N; J + +){ if(Map[i][j] > Map[i][k] +Map[k][j]) {Map[i][j]= Map[i][k] +Map[k][j]; PRICE[I][J]= Price[i][k] +Price[k][j]; } Else if(Map[i][j] = = Map[i][k] +Map[k][j]) { if(Price[i][j] >price[i][k] +Price[k][j]) {Price[i][j]= Price[i][k] +Price[k][j]; } }}} cout<< Map[s][d] <<" "<<price[s][d];}
Tourism Planning (25)