博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT1030:Travel Plan
阅读量:4351 次
发布时间:2019-06-07

本文共 3972 字,大约阅读时间需要 13 分钟。

1030. Travel Plan (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Input
4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20
Sample Output
0 2 3 3 40 思路 迪杰斯特拉单源最短路径算法和dfs的结合应用,比较绕,开始写的时候感觉比较难。概括来说主要为以下两步: 1.首先通过Dijkstra算法将起始点到所有点的最短距离计算出来,并用一个pre数组保存下所有路径节点的前驱结点(可能有多个前驱点,因为最短路径可能不止一条)。 2.从终点开始用dfs回溯到起点,因为最短路径不止一条,所以用一个tmp记录下回溯的每一条路径,然后通过比较路径的消耗总权值确定消耗最小的那条最短路径。 代码
#include
#include
#include
using namespace std;const int Infmax = pow(2,30);int N,M,S,D;vector
> graph(502,vector
(502,Infmax)); //The graphvector
Distance(502,Infmax); //The shortest distance from start to every node(Dynamic Update)vector
> pre(502); //a node's previous nodes with a shortest path.vector
visit(502,false); // check if the node is visitedvector
> cost(502,vector
(502,0)); //every road's costvector
path,tmp;//the final path and temp pathint 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}

 

转载于:https://www.cnblogs.com/0kk470/p/7743337.html

你可能感兴趣的文章
【随笔】入行必读:互联网行业薪酬等级
查看>>
Android使用开源框架加载图片
查看>>
CLR是怎么加载到内存的?
查看>>
fckeditor
查看>>
backbone.js
查看>>
python类的特殊成员变量
查看>>
sublime text3最新版本注册码(build 3143)
查看>>
linux使用技巧
查看>>
必背公式及常数
查看>>
利用CSS、JavaScript及Ajax实现图片预加载的三大方法
查看>>
EntityManager的merge()方法
查看>>
Spring中线程池的应用
查看>>
前端登录jq图形验证码
查看>>
软件设计
查看>>
Hadoop各种进程的配置文件及其位置说明
查看>>
js时间戳转时间格式
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Linux的用户态和内核态
查看>>
JavaScript原生错误及检测
查看>>
(原创) cocos2d-x 3.0+ lua 学习和工作(4) : 公共函数(3): 深度克隆clone()
查看>>