【PAT】1030. Travel Plan (30)【dijkstra算法】

题目描述

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 FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,开头包括4个正整数N,M,S,D,N(<=500) 代表城市的数量(城市被编号为0-N-1);M代表高速公路数;S和D分别代表起始点城市和终点城市。接下来M行每个根据以下格式提供一条高速公路的信息:
城市1 城市2 距离 费用
数字都不大于500,由空格隔开。

OUTPUT FORMAT

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算法完成即可,跟之前解法相同,可以参考一下我的【模板算法】单源最短路问题-dijkstra算法(邻接表+优先队列优化)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
struct Edge{
    int to,length,cost;
    Edge(){}
    Edge(int t,int l,int c):to(t),length(l),cost(c){}
    bool operator<(const Edge &a)const{
        return length==a.length?cost>a.cost:length>a.length;    
    }
}; 
vector<Edge> G[510];
priority_queue<Edge> q;
int N,M,S,D;
Edge d[510];
int pre[510];
int dijkstra(int s){
    for(int i=0;i<N;i++){
        d[i].length=INF;
        d[i].cost=INF;
        pre[i]=-1;
    }
    d[s].length=d[s].cost=0;
    q.push(Edge(s,0,0));
    while(!q.empty()){
        Edge temp=q.top();q.pop();
//      printf("%d %d %d\n",temp.to,temp.length,temp.cost);
        if(d[temp.to].length<temp.length)continue;
        for(int i=0;i<G[temp.to].size();i++){
            Edge e=G[temp.to][i];
            if(d[e.to].length>temp.length+e.length||(d[e.to].length==temp.length+e.length&&d[e.to].cost>temp.cost+e.cost)){
                d[e.to].length=temp.length+e.length;
                d[e.to].cost=temp.cost+e.cost;
                pre[e.to]=temp.to;
                q.push(Edge(e.to,d[e.to].length,d[e.to].cost));
            }
        }
    }
}
int ans[510],ccount=0;
void short_path(){
    int temp;
    temp=D;
    while(temp!=-1){
        ans[ccount++]=temp;
        temp=pre[temp];
    }
}
int main(){
    scanf("%d%d%d%d",&N,&M,&S,&D);
    int a,b,c,f;
    for(int i=0;i<M;i++){
        scanf("%d%d%d%d",&a,&b,&c,&f);
        G[a].push_back(Edge(b,c,f));
        G[b].push_back(Edge(a,c,f));
    }
    dijkstra(S);
    short_path();
    for(int i=ccount-1;i>=0;i--){
        printf("%d ",ans[i]);
    }
    printf("%d %d\n",d[D].length,d[D].cost);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值