1030. Travel Plan (30)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20Sample Output
0 2 3 3 40
思路
迪杰斯特拉单源最短路径算法和dfs的结合应用,比较绕,开始写的时候感觉比较难。概括来说主要为以下两步:
1.首先通过Dijkstra算法将起始点到所有点的最短距离计算出来,并用一个pre数组保存下所有路径节点的前驱结点(可能有多个前驱点,因为最短路径可能不止一条)。
2.从终点开始用dfs回溯到起点,因为最短路径不止一条,所以用一个tmp记录下回溯的每一条路径,然后通过比较路径的消耗总权值确定消耗最小的那条最短路径。
代码
#include<iostream> #include<vector> #include<math.h> using namespace std; const int Infmax = pow(2,30); int N,M,S,D; vector<vector<int>> graph(502,vector<int>(502,Infmax)); //The graph vector<int> Distance(502,Infmax); //The shortest distance from start to every node(Dynamic Update) vector<vector<int>> pre(502); //a node's previous nodes with a shortest path. vector<bool> visit(502,false); // check if the node is visited vector<vector<int>> cost(502,vector<int>(502,0)); //every road's cost vector<int> path,tmp;//the final path and temp path int mincost = Infmax; void dfs(int v) //track from the destination to start and find the min cost. { if(v == S) { tmp.push_back(v); int tmpcost = 0; for(int i = tmp.size() - 1;i > 0;i--) { tmpcost += cost[ tmp[i] ][ tmp[i - 1] ]; } if(tmpcost < mincost) { mincost = tmpcost; path = tmp; } tmp.pop_back(); return; } tmp.push_back(v); for(int i = 0;i < pre[v].size();i++) // dfs all of the previous nodes of this node dfs(pre[v][i]); tmp.pop_back(); } int main() { cin >> N >> M >> S >> D; for(int i = 0;i < M;i++) //Input { int s,d; cin >> s >> d; cin >> graph[s][d]; graph[d][s] = graph[s][d]; cin >> cost[s][d]; cost[d][s] = cost[s][d]; } //Confirm the start node pre[S].push_back(S); Distance[S] = 0; //Dijkstra's main algorithm for(int i = 0;i < N;i++) { int u = -1,nextmin = Infmax; for(int j = 0;j < N;j++) //Find the current shortest path from current node to next node { if(!visit[j] && Distance[j] < nextmin) { u = j; nextmin = Distance[j]; } } if(u == -1) break; //All of the Nodes have been visited visit[u] = true; for(int v = 0;v < N;v++) //search and update the shortest path of every node { if(!visit[v] && graph[u][v] != Infmax) { if(Distance[u] + graph[u][v] < Distance[v]) { Distance[v] = Distance[u] + graph[u][v]; pre[v].clear(); pre[v].push_back(u); } else if(Distance[u] + graph[u][v] == Distance[v]) { pre[v].push_back(u); } } } } dfs(D); //track back form the destination(D) to start(S) and find the min cost. //output for(int i = path.size()-1;i >=0;i--) //print the path cout << path[i] << " "; cout << Distance[D] << " " << mincost << endl; //print the shortest path's length and min cost }