题目: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 Dare 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
大致翻译:
现在有一个旅行者的地图,给定城市的点与路径长度mm,求两个城市之间的最短路径。典型的求最短路径的题目。
输入规则:
第一行:N,M,S和D
N是城市个数,从0到N-1进行标号,M是高速路的个数;S和D是开始以及结束的城市。
接下来是M行,这M行是高速路的信息,形式如下所示:
City1 City2 Distance Cost
输出规则:
对于每个测试用例,沿着从起点到目的地的最短路径沿城市打印一行,然后是总距离和路径的总成本。 数字必须用空格分隔,输出结尾必须没有多余的空格。
分析:
这题很明显需要用dijkstra算法先找到最短路径,再在这些最短路径中使用dfs找到cost最小的那一条。
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
bool visit[500]={false};
int e[500][500], cost[500][500];
int dis[500];//e中存储着从start到其的最短路径长度
vector<int> pre[500];
vector<int> path,temppath;
int n,m,start,destination;
int mincost=INT_MAX;
//深度优先搜索,每个节点的父节点存储在pre中了
void dfs(int v){
temppath.push_back(v);
if(v==start){
int tempcost = 0;
for(int i=temppath.size()-1;i>0;i--){
int id=temppath[i],nextid=temppath[i-1];
tempcost=tempcost+cost[id][nextid];
}
if(tempcost<mincost){
mincost=tempcost;
path=temppath;
}
temppath.pop_back();
return;
}
for(int i=0;i<pre[v].size();i++)
dfs(pre[v][i]);
temppath.pop_back();
}
int main(){
fill(e[0], e[0] + 510 * 510, INT_MAX);
fill(dis, dis + 510, INT_MAX);
int city1,city2,distance,co;
scanf("%d %d %d %d",&n,&m,&start,&destination);
for(int i=0;i<m;i++){
scanf("%d %d %d %d",&city1,&city2,&distance,&co);
e[city1][city2]=e[city2][city1]=distance;
cost[city1][city2]=cost[city2][city1]=co;
}
//使用dijkstra算法计算出到各点的最短路径
visit[start]=0;
dis[start]=0;
for(int i=0;i<n;i++){
int u=-1,minn=INT_MAX;
//找到新加入的点
for(int j=0;j<n;j++){
if(visit[j]==false&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
if(u==-1) break;
//对新加入的点做一些处理,存储各个点的最短路径
visit[u]=true;
for(int v=0;v<n;v++){
if(visit[v]==false && e[u][v]!=INT_MAX){
if(dis[u]+e[u][v]<dis[v]) {
//这里对dis进行更新。
dis[v]=dis[u]+e[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(dis[u]+e[u][v]==dis[v]){
pre[v].push_back(u);
}
}
}
}
dfs(destination);
for(int i=path.size()-1;i>=0;i--)
printf("%d ",path[i]);
printf("%d %d",dis[destination],mincost);
return 0;
}