7-10 旅游规划 (25分) (c++)

7-10 旅游规划 (25分)

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

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

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

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

输出样例:

3 40

整体思路照搬了一个博主:这里给个链接
https://www.cnblogs.com/Jie-Fei/p/9873419.html

我对代码的逻辑上做了小的修改。
如果有时间的话,我会再加注释

反正整体就是Dijkstra算法,鲁老师又不让用stl(好像有priority_queue可以优化时间复杂度)(我到现在好像还不太理解Dijkstra的思路,啊啊啊)
就直接用邻接矩阵的存储结构,就是复杂度好像是O(n^2)

#include <iostream>
#include <cstdlib>

using namespace std;
const int MVNUM = 1e3;
const int INF = 1e5;

//节点到源节点的最短距离,是否被访问,最小花费
struct{
    int visit, len, cost;
}Vertices[MVNUM];

struct {
    int len, cost;
}MGraph[MVNUM][MVNUM];

//网的初始权值是无穷
//初始化边的集合
void InitGraph(int N)
{
    for (int i = 0; i <= N; i++)
        for (int j = 0; j <= N; j++)
        {
            MGraph[i][j].len = INF;
            MGraph[i][j].cost = INF;
        }
}

void InputGraph(int M)
{
    int st, ed, len, cost;
    for (int i = 0; i < M; i++)
    {
        cin >> st >> ed >> len >> cost;
        MGraph[st][ed].len = len;
        MGraph[st][ed].cost = cost;
        MGraph[ed][st].len = len;
        MGraph[ed][st].cost = cost;
    }
}

void InitVertices(int N, int S)
{
    for (int i = 0; i <= N; i++)
    {
        Vertices[i].visit = 0;
        Vertices[i].len = MGraph[S][i].len;
        Vertices[i].cost = MGraph[S][i].cost;
    }
    Vertices[S].visit = 1;
    Vertices[S].len = 0;
    Vertices[S].cost = 0;
}
void Dijkstra(int N, int S)
{
    InitVertices(N, S);
    int cnt = N-1;
    //循环N-1次,因为要求N-1个节点到源节点的最短距离
    while(cnt--)
    {
        //更新MinVer
        int MinVer = N;
        for (int i = 0; i < N; i++)
        {
            if (!Vertices[i].visit && Vertices[i].len < Vertices[MinVer].len)
                MinVer = i;
        }
        //本题应该不存在这种情况 可不写(写了更逻辑更完整)
        if (MinVer == N) break;

        Vertices[MinVer].visit = 1;
        for (int i = 0; i < N; i++)
        {
            if (!Vertices[i].visit)
            {
                if (Vertices[i].len > Vertices[MinVer].len + MGraph[MinVer][i].len){
                    Vertices[i].len = Vertices[MinVer].len + MGraph[MinVer][i].len;
                    Vertices[i].cost = Vertices[MinVer].cost + MGraph[MinVer][i].cost;
                }
                else if(Vertices[i].len == Vertices[MinVer].len + MGraph[MinVer][i].len)
                {
                    if (Vertices[i].cost > Vertices[MinVer].cost + MGraph[MinVer][i].cost)
                    Vertices[i].cost = Vertices[MinVer].cost + MGraph[MinVer][i].cost;                        
                }
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int N, M, S, D;
    cin >> N >> M >> S >> D;
    InitGraph(N);
    InputGraph(M);
    Dijkstra(N, S);
    cout << Vertices[D].len << " " << Vertices[D].cost << endl;
    system("pause");
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值