1030. Travel Plan

Travel Plan

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条通路、每条通路的距离和花费,要求输出从起点到终点的最短路径以及具体长度和所需花费,如果距离相等则选择花费更小的路径。

思路

典型的最短路径问题。使用Dijkstra算法,可参考《Dijkstra算法》。在最短路径的基础上增加了对第二边权和的判断。


代码实现

#include <cstdio>

const int maxn = 501;
const int INF = 1000000000;

int g[maxn][maxn], cost[maxn][maxn], n;     // 距离邻接矩阵,花费邻接矩阵,结点数
int d[maxn], c[maxn];       // 起始点到结点i的最短距离和最少花费
bool visit[maxn];
int pre[maxn];          // 记录最短路径上每个结点的前驱结点

void Dijkstra(int s)
{
    for (int i = 0; i < n; i++)         // 初始化
    {
        d[i] = INF;
        visit[i] = false;
        pre[i] = i;
    }
    d[s] = 0;
    c[s] = 0;

    for (int i = 0; i < n; i++)         // 找到距离最短的未访问结点
    {
        int index = -1, minD = INF;
        for (int j = 0; j < n; j++)
            if (visit[j] == false && d[j] < minD)
            {
                minD = d[j];
                index = j;
            }

        if (index == -1)        // 未找到,则起始点与所有未访问结点都不连通
            return;
        visit[index] = true;    // 找到,标记为已访问

        for (int j = 0; j < n; j++)     // 以结点index为新的接入点,更新信息
            if (visit[j] == false && g[index][j] < INF)
            {
                if (d[index] + g[index][j] < d[j])      // 以index为转接点能使距离更近
                {
                    d[j] = d[index] + g[index][j];      // 更新最短距离
                    c[j] = c[index] + cost[index][j];   // 更新最小花费
                    pre[j] = index;     // 记录前驱结点
                }
                else if (d[index] + g[index][j] == d[j] && c[index] + cost[index][j] < c[j])     // 距离相同则判断花费是否更小
                {
                    pre[j] = index;     // 更新前驱结点
                    c[j] = c[index] + cost[index][j];       // 更新最小花费
                }
            }
    }
}

void DFS(int s, int v)          // 递归输出最短路径
{
    if (s == v)
    {
        printf("%d", s);
        return;
    }

    DFS(s, pre[v]);
    printf(" %d", v);
}

int main()
{
    int m, S, D;
    int c1, c2;

    scanf("%d%d%d%d", &n, &m, &S, &D);

    for (int i = 0; i < n; i++)         // 邻接矩阵初始化
        for (int j = 0; j < n; j++)
            g[i][j] = cost[i][j] = INF;

    for (int i = 0; i < m; i++)         // 邻接矩阵读入数据
    {
        scanf("%d%d", &c1, &c2);
        scanf("%d%d", &g[c1][c2], &cost[c1][c2]);
        g[c2][c1] = g[c1][c2];      // 注意不要忘了这步
        cost[c2][c1] = cost[c1][c2];
    }

    Dijkstra(S);
    DFS(S, D);
    printf(" %d %d", d[D], c[D]);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值