1030. Travel Plan (30)【最短路+路径记忆】——PAT (Advanced Level) Practise

来源:互联网 发布:男生发型软件 编辑:程序博客网 时间:2024/06/05 19:00

题目信息

1030. Travel Plan (30)

时间限制400 ms
内存限制65536 kB
代码长度限制16000 B

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

解题思路

dijkstra算法同时记录路径

AC代码

#include <cstdio>using namespace std;const int MAX = 0x1fffffff;int mp[505][505];int mpcost[505][505];int v[505], vis[505], path[505], cost[505];int n, m, s, d, a, b, dis, cst;void dij(){    vis[s] = true;    for (int i = 0; i < n; ++i){        v[i] = mp[s][i];        cost[i] = mpcost[s][i];        path[i] = s;    }    while (true){        int tmin = MAX, tid = -1;        for (int i = 0; i < n; ++i){            if (!vis[i] && tmin > v[i]){                tmin = v[i];                tid = i;            }        }        if (tid != -1){            vis[tid] = true;            for (int i = 0; i < n; ++i){                if (!vis[i]){                    if (v[tid] + mp[i][tid] < v[i]){                        v[i] = v[tid] + mp[i][tid];                        path[i] = tid;                        cost[i] = cost[tid] + mpcost[i][tid];                    }else if (v[tid] + mp[i][tid] == v[i] && cost[tid] + mpcost[i][tid] < cost[i]){                        path[i] = tid;                        cost[i] = cost[tid] + mpcost[i][tid];                    }                }            }        }else{            break;        }    }}void out(int id){    if (id == s){        printf("%d", s);        return;    }    out(path[id]);    printf(" %d", id);}int main(){    scanf("%d%d%d%d", &n, &m, &s, &d);    for (int i = 0; i < n; ++i){        v[i] = MAX;        for (int j = 0; j < n; ++j){            mp[i][j] = mpcost[i][j] = (i == j)? 0:MAX;        }    }    for (int i = 0; i < m; ++i){        scanf("%d%d%d%d", &a, &b, &dis, &cst);        mp[a][b] = mp[b][a] = dis;        mpcost[a][b] = mpcost[b][a] = cst;    }    dij();    out(d);    printf(" %d %d\n", v[d], cost[d]);    return 0;}
0 0
原创粉丝点击