1030 Travel Plan(30 分)
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
这题写了好久也木有写出来 搜了下博客get新姿势 注意重边
转载来自:https://blog.csdn.net/zjfclh/article/details/21296283
代码:
#include<cstdio>
#include<stack>
#define N 500
#define INF 0x3f3f3f3f
using namespace std;
int dist[N][N], price[N][N]; //使用邻接矩阵存储
int set[N]; //标志是否被访问过
int dis[N], pri[N]; //存放起点到各点的最短距离和花费
int path[N]; //存储起点到各个点最短路径中的倒数第二个点
int sta[1000]; //利用数组模拟棧进行路径输出
int main() {
int n, m,s, t;
scanf("%d %d %d %d",&n,&m,&s,&t);
int i, j;
int a, b, d, p;
//初始化各边的距离和花费最大
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
dist[i][j] = dist[j][i] = INF;
price[i][j] = price[j][j] = INF;
}
}
//当有重边时,更新相关信息
while(m--) {
scanf("%d %d %d %d",&a,&b,&d,&p);
if(dist[a][b] > d) {
dist[a][b] = dist[b][a] = d;
price[a][b] = price[b][a] = p;
}
if(dist[a][b] == d && p<price[a][b]) {
dist[a][b] = dist[b][a] = d;
price[a][b] = price[b][a] = p;
}
}
//初始化起点到各点的最短距离和花费以及访问标志
for(i=0; i<n; i++) {
dis[i] = dist[s][i];
pri[i] = price[s][i];
set[i] = 0;
if(dis[i]<INF)
path[i] = s; //嗯有道理 如果说起点到这个点的距离小于INF说明 他们是存在的,即那个东东的起点就是s
else
path[i] = -1;
}
set[s] = 1, path[s] = -1; //起点标志为1,表示已访问
for(i=0; i<n; i++)
{
int pre_sd = INF; //存储未被访问点到起点距离的最小值
int midp = -1; //存储未被访问点到起点距离的最小的点
for(j=0; j<n; j++) {
if(set[j]==0 && dis[j]<pre_sd) {
pre_sd = dis[j];
midp = j;
}
}
if(midp == -1)
break;
set[midp] = 1; //标志为1,表示已访问
for(j=0; j<n; j++) {
if(set[j]==0 ) { //以midp为中介点,更新各未被访问点到起点的距离和花费
if(dis[midp]+dist[midp][j] < dis[j]) {
dis[j] = dis[midp]+dist[midp][j];
pri[j] = pri[midp]+price[midp][j];
path[j] = midp; //将midp作为最短路径上倒数第二个节点
}
if(dis[midp]+dist[midp][j] == dis[j] && pri[j] > pri[midp]+price[midp][j]) {
dis[j] = dis[midp]+dist[midp][j];
pri[j] = pri[midp]+price[midp][j];
path[j] = midp;
}
}
}
}
int tmp = t;
stack<int>st;
st.push(t);
while(path[tmp] != -1) {
st.push(path[tmp]);
tmp = path[tmp];
}
while(st.empty()==0)
{
printf("%d ",st.top());
st.pop();
}
printf("%d %d\n",dis[t],pri[t]);
return 0;
}