手撕算法系列----Dijkstra单源最短路径

手撕算法系列----Dijkstra单源最短路径

前言

纯小白手撕一波dijkstra,十分简陋,有问题望指出。

理论

暂无理论哈哈哈哈(ps:懒得写,主要图好难画,下次一定补上!)
给个链接你们看链接

代码

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;

#define GRAPH_INFINITY 65535 /* 无穷大 */
#define MAXVEX 5 /*最大图顶点*/
#define MAXEDGE 20 /*最大图边*/


vector<vector<int>> createGraph()
{
    vector<vector<int>> graph(MAXVEX,vector<int>(MAXVEX,0));
    int n = graph.size();
    int m = graph[0].size();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < i+1; j++)
        {
            graph[i][j] = rand()%10;
            if (graph[i][j] == 0)    graph[i][j] = 1;
            graph[j][i] = graph[i][j];
        }
    }
    return graph;
}
int main()
{
    vector<vector<int>> graph = createGraph();
    int n = graph.size();
    int m = graph[0].size();
    graph[0][0] = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << graph[i][j] << " ";
        }
        cout << endl;
    }
    
    vector<int> vertex(n,0);
    vector<int> distance(n);
    queue<int> q;

    for (int i = 0; i < n; i++)
    {
        distance[i] = GRAPH_INFINITY;
    }

    vertex[0] = 1;
    distance[0] = 0;
    q.push(0);

    while (!q.empty())
    {
        int q_top = q.front();
        q.pop();
        int min_val = GRAPH_INFINITY + 1;
        for (int i = 0; i < n; i++)
        {

            if (graph[q_top][i] != -1 && vertex[i] != 1)
            {
                distance[i] = min(distance[i], graph[q_top][i] + distance[q_top]);
                min_val = min(min_val, distance[i]);
            }
        }
        for (int j = 0; j < n; j++)
        {
            if (min_val == distance[j] && vertex[j] != 1)
            {
                vertex[j] = 1;
                q.push(j);
                cout << "本轮选取" << j << endl;
                break;
            }
        }
        cout << "distance:" << " ";
        for (int i = 0; i < n; i++)
        {
            cout << distance[i] << " ";
        }
        cout << endl;

    }

    for (int i = 0; i < n; i++)
   // {
   //     cout << distance[i] << " ";
   // }


    return 0;
}

相关推荐

  1. 算法系列----Dijkstra路径

    2024-03-10 02:24:02       44 阅读
  2. 短路问题之Dijkstra算法 洛谷 路径

    2024-03-10 02:24:02       37 阅读
  3. 洛谷 P4779 [模板] 路径 题解 dijkstra算法

    2024-03-10 02:24:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 02:24:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 02:24:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 02:24:02       82 阅读
  4. Python语言-面向对象

    2024-03-10 02:24:02       91 阅读

热门阅读

  1. 生活里的英语应该【怎么说】

    2024-03-10 02:24:02       61 阅读
  2. 探索1688 API接口:实现商品数据自动化处理

    2024-03-10 02:24:02       42 阅读
  3. OpenFeign的学习总结

    2024-03-10 02:24:02       34 阅读
  4. QWebEngineView的load和seturl函数

    2024-03-10 02:24:02       48 阅读
  5. 朴素贝叶斯基本原理&sklearn实现

    2024-03-10 02:24:02       35 阅读
  6. oracle归档日志清理

    2024-03-10 02:24:02       40 阅读
  7. 数据库基础知识记录

    2024-03-10 02:24:02       41 阅读
  8. 用skopeo检查docker image

    2024-03-10 02:24:02       46 阅读
  9. uniApp h5解决Chrome跨域问题的最终方法

    2024-03-10 02:24:02       50 阅读