我是靠谱客的博主 务实故事,最近开发中收集的这篇文章主要介绍北京地铁票价查询系统 c++ Dijkstra算法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

包含一号线、二号线全部,还有两小段
数据来自北京地铁官网
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

资源链接

https://download.csdn.net/download/renzemingcsdn/44352688

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
using namespace std;

const int MAX_N = 99;
const int INF = 999999;

// 各数组都从下标1开始
int dst[MAX_N];     // 表示当前点到源点的最短路径长度
int prv[MAX_N];     // 记录当前点的前一个结点
int c[MAX_N][MAX_N];   // 记录图的两点间路径长度
int n=46, line=0;             // 图的结点数和路径数


void Dijkstra(int n, int v)
{
    bool vis[MAX_N];    // 判断是否已存入该点到S集合中
    for (int i = 1; i <= n; ++i)
    {
        dst[i] = c[v][i];
        vis[i] = 0;     // 初始都未用过该点
        if (dst[i] == INF)
            prv[i] = 0;
        else
            prv[i] = v;
    }
    dst[v] = 0;
    vis[v] = 1;

    // 依次将未放入S集合的结点中,取dst[]最小值的结点,放入结合S中
    // 一旦S包含了所有V中顶点,dst就记录了从源点到所有其他顶点之间的最短路径长度
         // 注意是从第二个节点开始,第一个为源点
    for (int i = 2; i <= n; ++i)
    {
        int tmp = INF;
        int u = v;
        // 找出当前未使用的点j的dst[j]最小值
        for (int j = 1; j <= n; ++j)
            if ((!vis[j]) && dst[j] < tmp)
            {
                u = j;              // u保存当前邻接点中距离最小的点的号码
                tmp = dst[j];
            }
        vis[u] = 1;    // 表示u点已存入S集合中

        // 更新dst
        for (int j = 1; j <= n; ++j)
            if ((!vis[j]) && c[u][j] < INF)
            {
                int newdst = dst[u] + c[u][j];
                if (newdst < dst[j])
                {
                    dst[j] = newdst;
                    prv[j] = u;
                }
            }
    }
}
void c_init() {
    for (int i = 0; i < MAX_N; i++) {
        dst[i] = INF;
        for (int j = 0; j < MAX_N; j++) {
            c[i][j] = INF;
        }
    }
    c[1][2] = c[2][1] = 2606;
    c[2][3] = c[3][2] = 1921;
    c[3][4] = c[4][3] = 1953;
    c[4][5] = c[5][4] = 1479;
    c[5][6] = c[6][5] = 1810;
    c[6][7] = c[7][6] = 1778; 
    c[7][8] = c[8][7] = 1313;
    c[8][9] = c[9][8] = 1172;
    c[9][10] = c[10][9] = 1166;
    c[10][11] = c[11][10] = 1291; 
    c[11][12] = c[12][11] = 424;
    c[12][13] = c[13][12] = 1590;
    c[13][14] = c[14][13] = 1217;
    c[14][15] = c[15][14] = 925;
    c[15][16] = c[16][15] = 852;
    c[16][17] = c[17][16] = 774;
    c[17][18] = c[18][17] = 1230;
    c[18][19] = c[19][18] = 1372;
    c[19][20] = c[20][19] = 790;
    c[20][21] = c[21][20] = 1385;
    c[21][22] = c[22][21] = 1673;
    c[22][23] = c[23][22] = 1714;
    c[12][24] = c[24][12] = 1832;
    c[24][25] = c[25][24] = 960;
    c[25][26] = c[25][26] = 909;
    c[26][27] = c[27][26] = 1899;
    c[27][28] = c[28][27] = 1766;
    c[28][29] = c[29][28] = 1237;
    c[29][30] = c[30][29] = 794;
    c[30][31] = c[31][30] = 2228;
    c[31][32] = c[32][31] = 824;
    c[32][33] = c[33][32] = 1024;
    c[33][18] = c[18][33] = 1763;
    c[34][18] = c[18][34] = 945;
    c[34][35] = c[35][34] = 1023;
    c[35][36] = c[36][35] = 1034;
    c[36][37] = c[37][36] = 1171;
    c[37][38] = c[38][37] = 851;
    c[38][39] = c[39][38] = 939;
    c[12][39] = c[39][12] = 1234;
    c[26][40] = c[40][26] = 1025;
    c[40][41] = c[41][40] = 1100;
    c[41][42] = c[42][41] = 1100;
    c[42][43] = c[43][42] = 869;
    c[43][13] = c[13][43] = 1011;
    c[13][38] = c[38][13] = 815;
    c[30][44] = c[44][30] = 866;
    c[44][45] = c[45][44] = 791;
    c[45][46] = c[46][45] = 1016;
    c[46][47] = c[47][46] = 848;
    c[47][17] = c[17][47] = 945;
    c[17][35] = c[35][17] = 821;
}
map<string, int> mp_stoi = {
{"pingguoyuan",1},
{"gucheng",2},
{"bajiao amusement park",3},
{"babaoshan",4},
{"yuquanlu",5},
{"wukesong",6},
{"wanshoulu",7},
{"gongzhufen",8},
{"militayr museum",9},
{"muxidi",10},
{"nanlishilu",11},
{"fuxingmen",12},
{"xidan",13},
{"tian anmen west",14},
{"tian anmen east",15},
{"wangfujing",16},
{"dongdan",17},
{"jianguomen",18},
{"yonganli",19},
{"guomao",20},
{"dawanglu",21},
{"sihui",22},
{"sihui east",23},

{"fuchengmen",24},
{"chegongzhuang",25},
{"xizhimen",26},
{"jishuitan",27},
{"guloudajie",28},
{"andingmen",29},
{"yonghegong",30},
{"dongzhimen",31},
{"dongshisitiao",32},
{"chaoyangmen",33},
{"beijingzhan",34},
{"chongwenmen",35},
{"qianmen",36},
{"hepingmen",37},
{"xuanwumen",38},
{"changchunjie",39},
{"xinjiekou",40},
{"pinganli",41},
{"xisi",42},
{"lingjinghutong",43},
{"beixinqiao",44},
{"zhangzizhognlu",45},
{"dongsi",46},
{"dengshikou",47},
};
int main()
{
    c_init();
    string s;
    getline(cin, s);
    int i;
    for (i = 0; i < s.size(); i++) {
        if (s[i] == ';') {
            break;
        }
    }
    string s1, s2;
    s1=s.substr(0,i);
    s2 = s.substr(i + 1, s.size()-1);
    int start, end;
    start = mp_stoi[s1];
    end = mp_stoi[s2];
    if (start == 0 || end == 0) {
        return -1;
    }
    Dijkstra(47, start);
    int l = dst[end];
    if (l <= 6000) {
        cout << 3 << endl;
    }
    else if (l <= 12000) {
        cout << 4 << endl;
    }
    else if (l <= 22000) {
        cout << 5 << endl;
    }
    else if (l <= 32000) {
        cout << 6 << endl;
    }
    else {
        cout << (6 + (l - 32) % 20 + 1) << endl;
    }
}

最后

以上就是务实故事为你收集整理的北京地铁票价查询系统 c++ Dijkstra算法的全部内容,希望文章能够帮你解决北京地铁票价查询系统 c++ Dijkstra算法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部