浙大数据结构习题笔记:Dijkstra算法(有权图最短路)

Dijkstra算法(有权图最短路)

具体教程内容见慕课
具体代码为可执行版,按有权图模式规范化输入即可,输出第一行为各行dict代表某结点从原点过来的总权重,输出第二行path为上一个结点的路径

具体代码实现

#include<iostream>
#include<stdlib.h>
#define Inf 1000000
#define Init -1
#define MaxVertex 100
typedef int Vertex;
int G[MaxVertex][MaxVertex];
int dist[MaxVertex];  // 距离
int path[MaxVertex];  // 路径
int collected[MaxVertex];  // 被收录集合
int Nv;   // 顶点
int Ne;   // 边
using namespace std;

// 初始化图信息
void build(){
    Vertex v1,v2;
    int w;
    printf("Enter Nv:");
    cin>>Nv;
    // 初始化图
    for(int i=1;i<=Nv;i++)
        for(int j=1;j<=Nv;j++)
            G[i][j] = 0;
    // 初始化路径
    for(int i=1;i<=Nv;i++)
        path[i] = Init;
    // 初始化距离
    for(int i=0;i<=Nv;i++)
        dist[i] = Inf;
    // 初始化收录情况
    for(int i=1;i<=Nv;i++)
        collected[i] = false;
    printf("Enter Ne:");
    cin>>Ne;
    // 初始化点
    for(int i=0;i<Ne;i++){
        printf("Enter each edge:");
        cin>>v1>>v2>>w;
        G[v1][v2] = w;  // 有向图
    }
}

// 初始化距离和路径信息
void create(Vertex s){
    dist[s] = 0;
    collected[s] = true;
    for(int i=1;i<=Nv;i++)
        if(G[s][i]){
            dist[i] = G[s][i];
            path[i] = s;
        }
}

// 查找未收录顶点中dist最小者
Vertex FindMin(Vertex s){
    int min = 0;  // 之前特地把 dist[0] 初始化为正无穷
    for(Vertex i=1;i<=Nv;i++)
        if(i != s && dist[i] < dist[min] && !collected[i])
            min = i;
    return min;
}


void Dijkstra(Vertex s){
    create(s);
    while(true){
        Vertex V = FindMin(s);   // 找到
        if(!V)
            break;
        collected[V] = true;  //收录
        for(Vertex W=1;W<=Nv;W++)
            if(!collected[W] && G[V][W]){  // 如果未被收录
                if(dist[V] + G[V][W] < dist[W]){
                    dist[W] = G[V][W] + dist[V];
                    path[W] = V;
                }
            }
    }
}

void output(){
    for(int i=1;i<=Nv;i++)
        cout<<dist[i]<<" ";
    cout<<endl;
    for(int i=1;i<=Nv;i++)
        cout<<path[i]<<" ";
    cout<<endl;
}


int main(){
    build();
    Dijkstra(1);
    output();
    return 0;
}

效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构习题Dijkstra 1.3、将二叉树看作,并对它作的深度优先遍历,则与原二叉树的 结果是相同的。 A、前序遍历 B、中序遍历C、后序遍历D、层次序遍历 1.4、在关于树的以下4种叙述中,正确的是 。 A、用指针方式存储有n个结点的二叉树,至少要有n+1个指针。 B、m阶B-树中,具有k个子结点的结点,必有k-1个键值。 C、m阶B-树中,每个非叶子结点的子结点个数≥[m/2]。 D、平衡树一定是丰满树。 1.5、在最好和最坏情况下的时间复杂度均为O(nlog2n)且稳定的排序方法是 A、希尔排序 B、快速排序 C、堆排序 D、归并排序 二、解答题 2.1、对目标串 abaabcc和模式串aabc,描述根据KMP算法进行匹配的过程,包括失效函数计算。答:失效函数:-1, 0, -1, -1 目标串 abaabcc和模式串aabc的KMP算法进行匹配的过程 abaabcc aabc 首先,从目标位置0和模式位置0开始比较,至ab和aa,不等时目标位置为1, 模式位置为1。因0位置失效函数的值( f [posP-1] ) 为-1,从目标位 置1和模式位置0开始新一轮比较。因第一个字符( posP == 0 )就不等,目标 位置增1,目标位置2和模式位置0开始新一轮比较,直到模式位置比较至4, 匹配成功。 int fastFind ( char *s, char * pat ){ //带失效函数的KMP匹配算法 int posP = 0, posT = 0; int lengthP = strlen(pat), lengthT = strlen(s); while ( posP < lengthP && posT < lengthT ) if ( pat[posP] == s[posT] ) { posP++; posT++; //相等继续比较 } else if ( posP == 0 ) posT++; //不相等 else posP = f [posP-1]+1; if ( posP < lengthP ) return -1; else return posT - lengthP; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值