题目:
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
题意:
第一行是四个数字,n,m,s,d,分别代表有n个城市,有m条路,起点城市s和终点城市d;
后面跟着m行数据,代表道路的情况,每一行四个数字,City1 City2 Distance Cost,让你求出来由起点到终点的最短的路径,如果距离相等时选择花费最小的道路,最后输出道路和最短的距离和花费;
思路:
以前做过一道题,也是双权最短路,但是,这道题又多了路径的输出,只需要改改代码即可。
《最短路径问题》博客链接:https://blog.csdn.net/titi2018815/article/details/81672564
当然了,这道题也是用dijkstra算法来进行求解,记得在最短路径的判断中加入对于花费的判断(即,当距离一样时,选择花费更小的路径);
对于路径的存储,用一个数组,每一次路径变化时,只需要存下来这个城市是由哪个城市来的,最后一个递归回溯从终点城市回溯输出即可。
代码如下:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<climits>
#include<string.h>
using namespace std;
const int MAX=0x3f3f3f3f;
int map[555][555];//存储地图;
int value[555][555];//存储花费;
int dis[555];//存储i到开始城市的距离;
int val[555];//存储i到开始城市的花费;
int vis[555];//存储这个城市是否加入了最短路中;
int pass[555];//存储路径;
void dfs(int v)//对于路径的回溯;
{
if(v==pass[v])//碰到了起点,结束搜索,开始回溯;
{
printf("%d ",v);
return;
}
dfs(pass[v]);
printf("%d ",v);
return ;
}
void Dijkstra(int s,int n)//最短路算法;
{
int i,j,k,min;
dis[s]=0;//起点到起点的距离是0;
val[s]=0;//起点到起点的花费是0;
pass[s]=s;//把起点的前驱节点定义成自己;
for(i=0; i<n; i++)//n个城市,寻找n-1条路;
{
min=MAX;
k=-1;
for(j=0; j<n; j++)//找到没有加入最短路中的最短的路;
{
if(vis[j]==0&&min>dis[j])
{
min=dis[j];
k=j;
}
}
if(k==-1)
return ;
vis[k]=1;//加入最短路;
for(j=0; j<n; j++)//路径进行替换改变;
{
if(vis[j]==0&&map[k][j]<MAX)//剪枝,防止超时;
{
if(dis[j]>dis[k]+map[k][j])//经过城市k中转后进路变小了,那么就进行把起点到城市j的距离改变的操作;
{
pass[j]=k;//路径改变;即城市j的前驱城市是k;
dis[j]=dis[k]+map[k][j];//把起点到城市j的距离改变;
val[j]=val[k]+value[k][j];//把起点到城市j的花费改变;
}
else if(dis[j]==dis[k]+map[k][j]&&val[j]>val[k]+value[k][j])//距离相同就选择最小的花费
{
pass[j]=k;//改变路径;
val[j]=val[k]+value[k][j];//改变花费;
}
}
}
}
return ;
}
int main()
{
int n,m;
int s,t;
while(~scanf("%d%d%d%d",&n,&m,&s,&t))
{
int a,b,d,p;
memset(vis,0,sizeof(vis));//初始化;
memset(map,MAX,sizeof(map));
memset(value,MAX,sizeof(value));
memset(dis,MAX,sizeof(dis));
memset(pass,0,sizeof pass);
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d",&a,&b,&d,&p);
map[a][b]=d;//双向图;
map[b][a]=d;
value[a][b]=p;
value[b][a]=p;
}
Dijkstra(s,n);
dfs(t);//路径回溯;
printf("%d %d\n",dis[t],val[t]);
}
return 0;
}