代码随想录-训练营-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星算法了。

相关推荐
小程故事多_806 小时前
从A2C、TRPO、PPO到GRPO,强化学习策略梯度算法完整演进与大模型落地实战解析
人工智能·算法
2601_956121977 小时前
二分算法(知识点+题目)
c++·算法
AndrewHZ10 小时前
【LLM技术全景】阶段总结:技术原理篇核心知识回顾
人工智能·深度学习·算法·语言模型·大模型·llm·芯片开发
小星星闪亮登场11 小时前
2026萌新联赛第三场-- (郑州轻工业大学)
数据结构·c++·经验分享·算法·贪心算法·排序算法·深度优先
冻柠檬飞冰走茶11 小时前
PTA基础编程题目集 7-8超速判断(C++语言实现)
开发语言·数据结构·c++·算法
玖玥拾12 小时前
LeetCode 88 合并两个有序数组
算法·leetcode
数据皮皮侠AI12 小时前
上市公司数字供应链金融指数(2010-2024)
大数据·人工智能·算法
小程故事多_8012 小时前
用GRPO算法重塑多智能体系统,从原理落地到复杂任务规划实战
人工智能·算法
Hi李耶13 小时前
【LeetCode】541.反转字符串 II
算法·leetcode·职场和发展
shylyly_13 小时前
104.二叉树的最大深度
数据结构·算法·104.二叉树的最大深度