代码随想录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。
今日总结
补卡中(),但是完结!!
