代码随想录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。

今日总结

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

相关推荐
七颗糖很甜几秒前
“十五五”气象发展规划:聚焦五大核心任务
大数据·python·算法
科研前沿7 分钟前
镜像视界浙江科技有限公司的关键技术突破有哪些?
大数据·人工智能·科技·算法·音视频·空间计算
嫩萝卜头儿16 分钟前
2 - 复杂度收尾 + 链表经典OJ
数据结构·算法·链表·复杂度
星马梦缘29 分钟前
算法设计与分析 作业二 答案与解析
算法·图论·dfs·bfs·floyd-warshall·bellman_ford·多源最短路
玛丽莲茼蒿29 分钟前
Leetcode hot100 每日温度【中等】
算法·leetcode·职场和发展
cjp56037 分钟前
009.UG二次开发,任务环境草图优化3(高级功能生成直线)
算法
样例过了就是过了1 小时前
LeetCode热题100 分割等和子集
数据结构·c++·算法·leetcode·动态规划
逻辑驱动的ken1 小时前
Java高频面试考点18
java·开发语言·数据库·算法·面试·职场和发展·哈希算法
北顾笙9801 小时前
day38-数据结构力扣
数据结构·算法·leetcode
m0_629494731 小时前
LeetCode 热题 100-----14.合并区间
数据结构·算法·leetcode