代码随想录-训练营-day52

97. 小明逛公园 (kamacoder.com)

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;
int main(){
    int n,m,u,v,w;
    cin>>n>>m;
    vector<vector<vector<int>>> grid(n+1,vector<vector<int>>(n+1,vector<int>(n+1,10001)));
    while(m--){
        cin>>u>>v>>w;
        grid[u][v][0]=w;
        grid[v][u][0]=w;
    }
    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][k][k-1]+grid[k][j][k-1],grid[i][j][k-1]);
            }
        }
    }
    int num,start,end;
    cin>>num;
    while(num--){
        cin>>start>>end;
        if(grid[start][end][n]==10001){
            cout<<-1<<endl;
        }
        else{
            cout<<grid[start][end][n]<<endl;
        }
    }
    return 0;
}

之前我们涉及的迪杰斯特拉算法和贝尔曼福德算法都是针对单源最短路径的,而当问题来到多源的最短路径时这两种算法就不适用了。针对多源最短路径问题,我们一般用floyd算法或者A星算法。

127. 骑士的攻击 (kamacoder.com)

cpp 复制代码
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int moves[1001][1001];
int b1,b2;
const vector<pair<int,int>> dirs={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}};
struct Knight{
    int x,y;
    int g,h,f;
    bool operator < (const Knight& k)const{
        return k.f<f;
    }
};
priority_queue<Knight> pq;
int OlaDis(const Knight& k){
    return (k.x-b1)*(k.x-b1)+(k.y-b2)*(k.y-b2);
}
void astar(const Knight& k){
    Knight cur,nex;
    pq.push(k);
    while(!pq.empty()){
        cur=pq.top();
        pq.pop();
        if(cur.x==b1&&cur.y==b2)break;
        for(auto [dx,dy]:dirs){
            nex.x=cur.x+dx;
            nex.y=cur.y+dy;
            if(nex.x<1||nex.x>1000||nex.y<1||nex.y>1000)continue;
            if(!moves[nex.x][nex.y]){
                moves[nex.x][nex.y]=moves[cur.x][cur.y]+1;
                nex.g=cur.g+5;
                nex.h=OlaDis(nex);
                nex.f=nex.g+nex.h;
                pq.push(nex);
            }
        }
    }
}
int main(){
    int n;
    cin>>n;
    int a1,a2;
    while(n--){
        cin>>a1>>a2>>b1>>b2;
        memset(moves,0,sizeof(moves));
        Knight start;
        start.x=a1;
        start.y=a2;
        start.g=0;
        start.h=OlaDis(start);
        start.f=start.g+start.h;
        astar(start);
        while(!pq.empty())pq.pop();
        cout<<moves[b1][b2]<<endl;
    }
    return 0;
}

这个就是A星算法了。

相关推荐
蜡笔小马4 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
唐梓航-求职中4 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
仟濹5 小时前
【算法打卡day3 | 2026-02-08 周日 | 算法: BFS】3_卡码网99_计数孤岛_BFS | 4_卡码网100_最大岛屿的面积DFS
算法·深度优先·宽度优先
Ll13045252985 小时前
Leetcode二叉树part4
算法·leetcode·职场和发展
颜酱5 小时前
二叉树遍历思维实战
javascript·后端·算法
宝贝儿好5 小时前
第二章: 图像处理基本操作
算法
小陈phd5 小时前
多模态大模型学习笔记(二)——机器学习十大经典算法:一张表看懂分类 / 回归 / 聚类 / 降维
学习·算法·机器学习
@––––––5 小时前
力扣hot100—系列4-贪心算法
算法·leetcode·贪心算法
CoovallyAIHub5 小时前
让本地知识引导AI追踪社区变迁,让AI真正理解社会现象
深度学习·算法·计算机视觉
CoderCodingNo5 小时前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法