当前位置: 首页 > news >正文

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

目录

    • Floyd 算法精讲
      • 解题思路
        • 解法1
        • 解法2
    • A * 算法精讲
      • 解题思路
        • 解法
    • 今日总结

Floyd 算法精讲

题目链接:KamaCoder
文章讲解:代码随想录

解题思路

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

解法1
#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(n3)O(n^3)O(n3)
  • 空间复杂度:O(n3)O(n^3)O(n3)
解法2

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

#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(n3)O(n^3)O(n3)
  • 空间复杂度:O(n2)O(n^2)O(n2)

A * 算法精讲

题目链接:KamaCoder
文章讲解:代码随想录

解题思路

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

解法
#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(n2)O(n^2)O(n2)
  • 空间复杂度:O(bd)O(b ^ d)O(bd)

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

今日总结

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

http://www.dtcms.com/a/529137.html

相关文章:

  • 网站开发难吗腾讯网站建设专家
  • 企业网站建设教程视频企业网络营销方案策划书
  • 网站服务器怎么选择做网站可以申请国家补助吗
  • 记一次k8s服务部署之后,访问返回503
  • 【Python】——注释
  • ASP.NET Core Blazor简介和快速入门三(布局和路由)
  • 基于微信小程序的博物馆文创系统
  • Claude Code更换供应商:Base URL和API Key的注意事项
  • 蛋白质组学技术揭秘帕金森病:LiP-MS、BAR、UltraID - LIPA 助力 α- 突触核蛋白研究
  • 网站基本模块管理系统网站模板下载
  • 网站错位免费秒玩小游戏
  • Windows编程+使用C++编写EXE加壳程序
  • 邢台做网站服务商网页平面设计要学什么
  • Vue3和Vue2的生命周期差异
  • 永不倒闭的10大央企seo网页优化平台
  • 做网站的技术支持蒙牛网站是谁做的
  • 河北手机网站制作价格电子商务网站建设 李洪心
  • 基于Double-LCC与NLC补偿网络的三发射-三接收非接触电能传输系统协同控制策略
  • 国外图床 wordpress海外seo网站建设
  • 企业如何利用AI CRM提升市场竞争力—以悟空AI CRM为例
  • Vue3 组件注册
  • 二手旧书网站开发设计报告开发公司以现金方式补贴给客户
  • AI 驱动的浏览器战争:OpenAI 的 Atlas 来了,安全漏洞却成隐患
  • 从零实现一个可加减的Matrix矩阵类:支持索引、相等判断与实际场景应用
  • CUDA-GDB(5)——内核聚焦
  • 51c大模型~合集29
  • 543刷题记录
  • 上海门户网站建设公司logo在线
  • 【FPGA】三八译码器——仿真验证
  • 哪些网站可以做设计方案网站建设 客户