代码随想录Day61|Floyd 算法精讲、A * 算法精讲

目录

Floyd 算法精讲

题目链接:KamaCoder

文章讲解:代码随想录

解题思路

  多源最短路径,本质上是动态规划。逐层求解两点之间的最短距离,每一层的可经过节点集合逐层累加,一开始赋值时集合为0,表示两点间的直线距离,之后集合k表示可以经过集合[1,2,...,k]中的点的最小距离,三层循环逐层计算。

解法1
cpp 复制代码
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<vector<int>>> grid(n+1, vector<vector<int>>(n+1, vector<int>(n+1, 10001)));
    for(int i=0; i<m; i++){
        int s, t, val;
        cin >> s >> t >> val;
        grid[s][t][0] = grid[t][s][0] = val;
    }

    for(int k=1; k<=n; k++){
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                grid[i][j][k] = min(grid[i][j][k-1], grid[i][k][k-1] + grid[k][j][k-1]);
            }
        }
    }

    int q;
    cin >> q;

    while(q--){
        int start, end;
        cin >> start >> end;
        if(grid[start][end][n]!=10001)    cout << grid[start][end][n] << endl;
        else    cout << -1 << endl;
    }
    return 0;
}
  • 时间复杂度: O ( n 3 ) O(n^3) O(n3)
  • 空间复杂度: O ( n 3 ) O(n^3) O(n3)
解法2

  可以看到这里第k层的计算只依赖第k-1层的结果。求解过程中确实可能会出现重复的情况,比如本层计算更新了[i][k],而后面过程中又以[i][k]更新了[i][j],本来应该用上一层的[i][k]来更新的。这样确实会导致每一层的更新结果和原先不一样,但这是不影响最后结果的,只是提前更新了而已,最终结果相同,因此可以做状态压缩。

cpp 复制代码
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> grid(n+1, vector<int>(n+1, 10001));
    for(int i=0; i<m; i++){
        int s, t, val;
        cin >> s >> t >> val;
        grid[s][t] = grid[t][s] = val;
    }

    for(int k=1; k<=n; k++){
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                grid[i][j] = min(grid[i][j], grid[i][k] + grid[k][j]);
            }
        }
    }

    int q;
    cin >> q;

    while(q--){
        int start, end;
        cin >> start >> end;
        if(grid[start][end]!=10001)    cout << grid[start][end] << endl;
        else    cout << -1 << endl;
    }
    return 0;
}
  • 时间复杂度: O ( n 3 ) O(n^3) O(n3)
  • 空间复杂度: O ( n 2 ) O(n^2) O(n2)

A * 算法精讲

题目链接:KamaCoder

文章讲解:代码随想录

解题思路

  灵活性很高的图论算法,具体表现取决于其测度函数的选取,本题中使用的是欧拉距离。实现起来其实和广度优先搜索很类似,不同的是A*算法每次取出的都是队列中测度最小的点,本质是小顶堆。这里优先队列的使用不太熟练,写代码的过程中有了更清晰的认识和理解。

解法
cpp 复制代码
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

int moves[1001][1001];
int b1, b2;

struct Knight{
    int x, y;
    int f, h, g;
    bool operator < (const Knight& k) const {
        return k.f < f;
    }
};

priority_queue<Knight> pq;

int dir[8][2] = {{2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2}, {1,2}, {2,1}};

int Heuristic(const Knight& k){
    return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2);
}

void Astr(const Knight& k){
    pq.push(k);
    Knight cur, next;
    while(!pq.empty()){
        cur = pq.top();
        pq.pop();

        if(cur.x == b1 && cur.y == b2)
            break;

        for(int i=0; i<8; i++){
            next.x = cur.x + dir[i][0];
            next.y = cur.y + dir[i][1];
            if(next.x < 1 || next.x > 1000 || next.y < 1 || next.y > 1000)
                continue;
            if(!moves[next.x][next.y]){
                next.h = Heuristic(next);
                next.g = cur.g + 5;
                next.f = next.h + next.g;
                pq.push(next);
                moves[next.x][next.y] = moves[cur.x][cur.y] + 1;
            }
            
        }
    }
}

int main(){
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        int a1, a2;
        cin >> a1 >> a2 >> b1 >> b2;
        memset(moves, 0, sizeof(moves));
        Knight start;
        start.x = a1; 
        start.y = a2;
        start.h = Heuristic(start);
        start.g = 0;
        start.f = start.h + start.g;
        Astr(start);
        
        while(!pq.empty()){
            pq.pop();
        }

        cout << moves[b1][b2] << endl;

    }
    return 0;
}
  • 时间复杂度: O ( n 2 ) O(n^2) O(n2)
  • 空间复杂度: O ( b d ) O(b ^ d) O(bd)

d 为起点到终点的深度,b 是 图中节点间的连接数量,本题因为是无权网格图,所以 节点间连接数量为 4。

今日总结

  补卡中(),但是完结!!

相关推荐
珊瑚里的鱼1 分钟前
【动态规划】买卖股票的最佳时机含手续费
算法·动态规划
2401_8856651923 分钟前
从零搭建卷积神经网络:基于PyTorch实现MNIST手写数字分类
pytorch·python·神经网络·算法·机器学习·分类·cnn
bIo7lyA8v23 分钟前
算法优化的多层缓存映射与访问调度模型的技术8
算法
dongf201933 分钟前
R语言朴素贝叶斯算法---iris数据集
开发语言·算法·数据分析·r语言
小O的算法实验室33 分钟前
2025年KBS,基于强化学习离散状态转移算法+复杂约束下多无人机任务分配
算法
weixin_3077791336 分钟前
从“大海捞针”到“主动推理”:AI如何重塑云原生故障诊断的根因链
开发语言·人工智能·算法·自动化·原型模式
京东云开发者39 分钟前
一键调用!京东云率先上线MiniMax M3
算法
papership1 小时前
入门级-数据结构-2、简单树:二叉树的遍历(前序、中序、后序)
数据结构·算法
WWW65261 小时前
代码随想录 打卡第五十四天
数据结构·c++·算法
happymaker06261 小时前
LeetCodeHot100——15.三数之和
数据结构·算法