题目来源:PAT (Advanced Level) Practice
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
words:
destination 目的地 separated 分开的 Travel 旅行
题意:
给出一个n个点和m条边的带权无向图,求起始点S到目的点D的最短路径及该路径的花费(若不唯一则选择花费最小的);
思路:
1. 使用邻接矩阵存储无向图;
2. 使用Dijkstra算法求最短路径,在求的过程中,在路径相等时选择花费最少的;
//PAT ad 1030 Travel Plan
#include <iostream>
#define N 505
using namespace std;
#include <vector>
#include <set>
#define MaxInt 65535
int n,m,s,d;
int dis[N]; //最短路径
int co[N]; //最小花费
int path[N]; //前驱
int vis[N]; //访问数组
void print_path(int s,int d) //回溯路径
{
if(path[d]==s)
cout<<s<<" ";
else
{
print_path(s,path[d]);
cout<<path[d]<<" ";
}
}
int main()
{
cin>>n>>m>>s>>d;
vector<vector<pair<int,int> > > adj(n,vector<pair<int,int> >(n,pair<int,int> (MaxInt,MaxInt))); //邻接矩阵
int i,c1,c2,distance,cost;
for(i=0;i<m;i++) //建立邻接矩阵
{
cin>>c1>>c2>>distance>>cost;
adj[c1][c2].first=adj[c2][c1].first=distance; //distance距离
adj[c1][c2].second=adj[c2][c1].second=cost; //cost花费
}
int v;
//Dijkstra算法开始了
for(i=0;i<n;i++) //做标记,定前驱
{
vis[i]=false;
dis[i]=adj[s][i].first; //距离
co[i]=adj[s][i].second; //花费
if(adj[s][i].first<MaxInt) path[i]=s;
else path[i]=-1;
}
vis[s]=true;
for(i=0;i<n-1;i++) // n-1次循环
{
int mi1=MaxInt;
int mi2=MaxInt;
for(int w=0;w<n;w++) //找最短
{
if(!vis[w]&&dis[w]<mi1)
{
v=w;mi1=dis[w];mi2=co[w];
}
else if(!vis[w]&&(dis[w]==mi1&&dis[w]<mi2))
{
v=w;mi1=dis[w];mi2=co[w];
}
}
vis[v]=true;
for(int w=0;w<n;w++)
{
if(!vis[w]&&(dis[v]+adj[v][w].first)<dis[w]) //间接距离小于直接距离
{
dis[w]=dis[v]+adj[v][w].first; //做更新 ,更新距离
co[w]=co[v]+adj[v][w].second;
path[w]=v;
}
else if(!vis[w]&&(dis[v]+adj[v][w].first)==dis[w]&&(co[v]+adj[v][w].second)<co[w]) //间接距离小于直接距离
{
dis[w]=dis[v]+adj[v][w].first; //做更新 ,更新距离
co[w]=co[v]+adj[v][w].second;
path[w]=v;
}
}
}
print_path(s,d); //打印最短路劲
cout<<d<<" "<<dis[d]<<" "<<co[d]<<endl;
return 0;
}