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 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output

0 2 3 3 40
 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int N, M, S, D;
const int INF = ;
int G[][], cost[][], visit[], dst[];
vector<int> pre[];
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i < N; i++){
int u = -, minLen = INF;
for(int j = ; j < N; j++){
if(visit[j] == && dst[j] < minLen){
minLen = dst[j];
u = j;
}
}
if(u == -){
return;
}
visit[u] = ;
for(int j = ; j < N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].push_back(u);
}
}
}
}
}
vector<int> tempPath, ans;
int minCost = INF;
void DFS(int d){
tempPath.push_back(d);
if(d == S){
int tempCost = ;
for(int i = tempPath.size() - ; i > ; i--){
tempCost += cost[tempPath[i]][tempPath[i - ]];
}
if(tempCost < minCost){
minCost = tempCost;
ans = tempPath;
}
tempPath.pop_back();
return;
}
int len = pre[d].size();
for(int i =; i < len; i++){
DFS(pre[d][i]);
}
tempPath.pop_back();
}
int main(){
fill(G[], G[] + *, INF);
scanf("%d%d%d%d", &N, &M, &S, &D);
int tempA, tempB, tempD, tempC;
for(int i = ; i < M; i++){
scanf("%d%d%d%d", &tempA, &tempB, &tempD, &tempC);
G[tempA][tempB] = G[tempB][tempA] = tempD;
cost[tempA][tempB] = cost[tempB][tempA] = tempC;
}
dijkstra(S);
DFS(D);
for(int i = ans.size() - ; i >= ; i--){
printf("%d ", ans[i]);
}
printf("%d %d", dst[D], minCost);
cin >> N;
return ;
}

总结:

1、题意:求出最短路径,如果有多条则求出花费的边权之和最短的一条。本题可采用dijkstra专门搜索最短路,将其存入pre数组。再用深搜遍历pre,计算、比较cost的花费并保存最优路径。

2、迪杰斯特拉:注意每次选择一个未优化的且距离最短的节点作为u,并将其visit设为1。注意在选择更新的节点也要是未被优化过的节点。 注意别忘记更新dst。

3、深搜注意push和pop的位置和时机。

最新文章

  1. PDO
  2. PHP多级联动的学习(二)
  3. Tomcat搭建
  4. 架构实例之Demo_JSP_JavaBean
  5. php查询文件扩展名
  6. 【poj2478】 Farey Sequence
  7. IIS 内部运行机制
  8. 2014年小结之sql语句优化
  9. C++二叉树的实现
  10. JPEG最优压缩参数试验【光影魔术手VS Image Optimizer】
  11. Delphi XE5 常用功具与下载
  12. MySQL ubuntu启动
  13. 【UVA】12299-RMQ with Shifts(线段树)
  14. Oracle之range,hash,list分区现实应用及优缺点汇总
  15. FusionCharts封装-Label
  16. WebServeice 动态代理类
  17. windows环境下安装yaf框架
  18. enquire.js-响应css媒体查询的轻量级javascript库
  19. asp。net内置委托
  20. Deepin 15.4 个性化设置

热门文章

  1. Day5-1 面向对象和面向过程
  2. shell自定义输入输出 read+echo
  3. 去掉dede织梦position当前位置最后一个箭头的方法
  4. [转]Java 的强引用、弱引用、软引用、虚引用
  5. oracle数据库备份和恢复
  6. TestNG之使用ReportNG生成测试报告
  7. A Simple Problem with Integers(线段树区间更新模板)
  8. 训练赛-Building Numbers
  9. 11/5/2018模拟 Problem C
  10. 因为网络安全的重要性打算学习linux