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