最短路问题之dijikstra算法

cpp 复制代码
//根据bfs修改而来
#include<stdio.h>
#include<stdlib.h>
typedef struct queue {
    int data;
    struct queue* next;
}queue, * linklist;
float dist_list[9];
//出发点为0
int forward_point_list[9] = { -1 };
linklist front = NULL;
linklist rear = NULL;
float map_frame[9][9] = { 0,4,-1,-1,-1,-1,-1,8,-1,
                       4,0,8,-1,-1,-1,-1,11,-1,
                       -1,8,0,7,-1,4,-1,-1,2,
                       -1,-1,7,0,9,14,-1,-1,-1,
                       -1,-1,-1,9,0,10,-1,-1,-1,
                       -1,-1,4,14,10,0,2,-1,-1,
                       -1,-1,-1,-1,-1,2,0,1,6,
                       8,11,-1,-1,-1,-1,1,0,7,
                       -1,-1,2,-1,-1,-1,6,7,0 };
void enqueue(int v) {
    linklist node = (linklist)malloc(sizeof(queue));
    node->data = v;
    node->next = NULL;
    if (!front) {
        front = rear = node;
    }
    else {
        rear->next = node;
        rear = node;
    }
}
int dequeue() {
    if (!front) {
        return -1;
    }
    linklist tmp = front;
    int v = front->data;
    front = front->next;
    if (!front) {
        rear = NULL;
    }
    free(tmp);
    return v;
}
bool isEmpty() {
    return front == NULL;
}
void DJSTRA(int v,int search) {
    enqueue(v);
    dist_list[v] = map_frame[v][v];
    forward_point_list[v] = v;
    while (isEmpty() == false) {
        v = dequeue();
        for (int i = 0; i < 9; i++) {
            if (map_frame[v][i] > 0) {
                if (dist_list[i] > dist_list[v] + map_frame[v][i]) {
                    dist_list[i] = dist_list[v] + map_frame[v][i];
                    forward_point_list[i] = v;
                    enqueue(i);
                }
            }
        }
    }
    for (int j = 0; j < 9; j++) {
        printf("%f\n", dist_list[j]);
    }
    while (search != 0) {
        printf("点%d由",search);
        search = forward_point_list[search];
        printf("%d而来\n",search);        
    }
 }
int main() {
    for (int j = 0; j < 9; j++) {
        dist_list[j] = 100.0;
    }
    DJSTRA(0,4);
    return 0;
}

相关推荐
雾月557 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
Pasregret26 分钟前
访问者模式:分离数据结构与操作的设计模式
数据结构·设计模式·访问者模式
知来者逆2 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北2 小时前
力扣-160.相交链表
算法·leetcode·链表
草莓啵啵~2 小时前
搜索二叉树-key的搜索模型
数据结构·c++
爱研究的小陈3 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing3 小时前
二叉树的最大宽度计算
算法·面试
丶Darling.3 小时前
26考研 | 王道 | 数据结构 | 第八章 排序
数据结构·考研·排序算法
BB_CC_DD3 小时前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类