贪心算法:单源最短路径(迪杰斯特拉Dijkstra算法)实现

输入

在这里插入图片描述

在这里插入图片描述
用邻接矩阵存储,并以文件的形式读入。

代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

void shortest_paths(std::vector<std::vector<int>> cost, int start) {
    // 某节点是否已被选择加入最短路径
    std::vector<bool> isSelected(cost.size(), false);
    // start 加入被选列表
    isSelected[start] = true;
    // dist[i] 表示当前start到i的距离
    std::vector<int> dist(cost.size(), 0);
    // 初始化dist
    for (int i = 0; i < cost.size(); ++i) {
        dist[i] = cost[start][i];
    }
    // 共循环n-2次,每次找出一条最短路
    for (int i = 0; i < cost.size() - 2; ++i) {
        // 选出使dist最小的节点,并且这个节点不能在已选列表里
        int minDist = INT_MAX, minPos = 0;
        for (int j = 0; j < dist.size(); ++j) {
            if (isSelected[j] == false && dist[j] < minDist) {
                minDist = dist[j];
                minPos = j;
            }
        }

        // 将选出的节点置为已选
        isSelected[minPos] = true;
        std::cout << "第" << i + 1 << "次:" << std::endl;
        for (int j = 0; j < isSelected.size(); ++j) {
            if (isSelected[j] == true) {
                std::cout << j << " ";
            }
        }
        std::cout << std::endl;

        // 更新dist
        for (int j = 0; j < dist.size(); ++j) {
            if (isSelected[j] == false) {
                dist[j] = std::min(dist[j], cost[minPos][j] + dist[minPos]);
            }
        }
        for (int j = 0; j < dist.size(); ++j) {
            std::cout << dist[j] << " ";
        }
        std::cout << std::endl;
    }
}

int main() {
    std::ifstream in = std::ifstream("file.txt");
    std::string s;
    std::vector<std::vector<int>> cost;
    int cnt = 0;
    while(std::getline(in, s)) {
        std::stringstream ss(s);
        std::vector<int> temp;
        while (!ss.eof()) {
            int d;
            ss >> d;
            if (d == -1) {
                d = INT_MAX - 100;
            }
            temp.push_back(d);
        }
        cost.push_back(temp);
        cnt++;
    }

    for (auto i : cost) {
        for (auto j : i) {
            std::cout << j << " ";
        }
        std::cout << std::endl;
    }

    shortest_paths(cost, 0);

    return 0;
}

输出

在这里插入图片描述

0 20 50 30 2147483547 2147483547 2147483547 
2147483547 0 25 2147483547 2147483547 70 2147483547
2147483547 2147483547 0 40 25 50 2147483547
2147483547 2147483547 2147483547 0 55 2147483547 2147483547
2147483547 2147483547 2147483547 2147483547 0 10 70
2147483547 2147483547 2147483547 2147483547 2147483547 0 50
2147483547 2147483547 2147483547 2147483547 2147483547 2147483547 01次:
0 1
0 20 45 30 2147483547 90 21474835472次:
0 1 3
0 20 45 30 85 90 21474835473次:
0 1 2 3
0 20 45 30 70 90 21474835474次:
0 1 2 3 4
0 20 45 30 70 80 1405次:
0 1 2 3 4 5
0 20 45 30 70 80 130
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伯明翰谢老二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值