【PAT】A1030 Travel Plan【Dijkstra算法】

旅行者的地图给出了城市之间的高速公路距离及其成本。编程帮助旅行者找出从起点到目的地的最短路径,如果最短路径不唯一,则输出成本最低的路径。输入包含城市数量、公路数量、起始城市和目标城市;输出最短路径及总距离和总成本。Dijkstra算法稍作修改,当遇到相同距离但成本更低的路线时更新路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路

这题相当于A1087的简化版,具体可以先解决A1087,这道题就很简单了。在Dijkstra上做一点小改动,当扫描到相同距离的路线时,如果新路线的cost更小,则更新路线。

代码

#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <limits.h>
#define MAX_N 500
struct Edge{
    int to;
    int dist;
    int cost;
    Edge(int to, int dist, int cost) : to(to), dist(dist), cost(cost){};
};
struct Node{
    int to;
    int dist;
    Node(int to, int dist) : to(to), dist(dist){};
};
bool operator < (Node a, Node b){
    return a.dist > b. dist;// 这里不要写错,我们需要定义的是升序的优先队列
}
using namespace std;
int main(){
    int N, M, S, D;
    scanf("%d %d %d %d", &N, &M, &S, &D);
    vector<vector<Edge>> G(N);
    for(int i = 0, u, v, d, c; i < M; i++){
        scanf("%d %d %d %d", &u, &v, &d, &c);
        G[u].push_back(Edge(v, d, c));
        G[v].push_back(Edge(u, d, c));
    }
    
    bool marked[MAX_N];
    int edgeTo[MAX_N], distTo[MAX_N], cost[MAX_N];
    fill(marked, marked + N, false);
    fill(edgeTo, edgeTo + N, -1);
    fill(distTo, distTo + N, INT_MAX);
    fill(cost, cost + N, 0);
    priority_queue<Node> pq;
    distTo[S] = 0;
    pq.push(Node(S, 0));
    
    // Dijkstra求最短路
    int u, v;
    while (!pq.empty()) {
        u = pq.top().to;
        pq.pop();
        if(marked[u]) continue;
        if(u == D) break;// 找到终点,退出,可以减少循环次数
        marked[u] = true;
        for(Edge& e : G[u]){
            v = e.to;
            if(marked[v]) continue;
            if(distTo[u] + e.dist < distTo[v]){
                distTo[v] = distTo[u] + e.dist;
                edgeTo[v] = u;
                cost[v] = cost[u] + e.cost;
                pq.push(Node(v, distTo[v]));
            }else if(distTo[u] + e.dist == distTo[v] && cost[u] + e.cost < cost[v]){// 找到距离相等,但是花费更少的路线,更新路线和花费
                edgeTo[v] = u;
                cost[v] = cost[u] + e.cost;
            }
        }
    }
    
    // 循环获取路径
    vector<int> path;
    for(int x = D; x != -1; x = edgeTo[x]){
        path.push_back(x);
    }
    
    for(int i = (int)path.size() - 1; i >= 0; i--){
        printf("%d ", path[i]);
    }
    printf("%d %d", distTo[D], cost[D]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值