前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >迪杰斯特拉算法 旅游规划

迪杰斯特拉算法 旅游规划

作者头像
叶茂林
发布2023-07-30 13:46:27
1560
发布2023-07-30 13:46:27
举报

题目描述

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

输入

输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

输出

在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

输入样例1 

4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20

输出样例1

3 40

AC代码

代码语言:javascript
复制
#include <iostream>
#include<vector>

using namespace std;
const int max_vertex_number = 500;

class Map {
    int matrix[max_vertex_number][max_vertex_number]={0};
    int matrix_cost[max_vertex_number][max_vertex_number]={0};
    int distance[max_vertex_number] = {0};
    int cost[max_vertex_number]={0};
    int vertex_number;
    string vertex[max_vertex_number];
    vector<int> path[max_vertex_number];
    int start,end;
    int edges=0;
    bool done[max_vertex_number] = {false};
public:
    Map() {
        cin >> vertex_number>>edges>> start>>end;
        for(int i=0;i<edges;i++){
            int tail,head,length,fee;
            cin>>head>>tail>>length>>fee;
            matrix[head][tail]=matrix[tail][head]=length;
            matrix_cost[head][tail]=matrix_cost[tail][head]=fee;
        }
    }

    void Dijkstra() {
        for (int i = 0; i < vertex_number; i++) {
            if (matrix[start][i]) {
                distance[i] = matrix[start][i];
                cost[i]=matrix_cost[start][i];
            }
        }
        done[start] = true;
        for (int i = 0; i < vertex_number - 1; i++) {
            int minDistance = 0x3f3f3f3f;
            int minIndex;
            int minCost=0x3f3f3f3f;
            for (int j = 0; j < vertex_number; j++)
                if (done[j] == false && distance[j] &&( distance[j] < minDistance||distance[j]==minDistance&&cost[j]<minCost)) {
                    minIndex = j;
                    minDistance = distance[j];
                    minCost=cost[j];
                }
            done[minIndex] = true;
            for (int j = 0; j < vertex_number; j++)
                if (matrix[minIndex][j] && done[j] == false &&
                    (minDistance + matrix[minIndex][j] < distance[j] || distance[j] == 0||minDistance+matrix[minIndex][j]==distance[j]&&minCost+matrix_cost[minIndex][j]<cost[j])) {
                    distance[j] = minDistance + matrix[minIndex][j];
                    cost[j]=minCost+matrix_cost[minIndex][j];
                }
        }
    }
    void Show(){
        Dijkstra();
        cout<<distance[end]<<' '<<cost[end];
    }
};

int main() {
    Map test;
    test.Show();
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-02-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档