最短路问题之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;
}

相关推荐
Boop_wu6 小时前
[数据结构] Map和Set
java·数据结构·算法
要一起看日出6 小时前
数据结构------二叉查找树
数据结构·二叉查找树
思考的笛卡尔7 小时前
密码学基础:RSA与AES算法的实现与对比
网络·算法·密码学
小许学java12 小时前
数据结构-ArrayList与顺序表
java·数据结构·顺序表·arraylist·线性表
格林威13 小时前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
程序员莫小特15 小时前
老题新解|大整数加法
数据结构·c++·算法
小刘max16 小时前
深入理解队列(Queue):从原理到实践的完整指南
数据结构
过往入尘土16 小时前
服务端与客户端的简单链接
人工智能·python·算法·pycharm·大模型
zycoder.16 小时前
力扣面试经典150题day1第一题(lc88),第二题(lc27)
算法·leetcode·面试
蒙奇D索大16 小时前
【数据结构】考研数据结构核心考点:二叉排序树(BST)全方位详解与代码实现
数据结构·笔记·学习·考研·算法·改行学it