A*算法的初步与k短路

A*(念做:A Star)算法是一种很常用的路径查找和图形遍历算法。它有较好的性能和准确度。

A*算法最初发表于1968年,由Stanford研究院的Peter Hart, Nils Nilsson以及Bertram
Raphael发表。它可以被认为是Dijkstra算法的扩展。

由于借助启发函数的引导,A*算法通常拥有更好的性能。

一些含义

  • open list:带扩展的格子。
  • close list:不会在被考虑的格子。
  • 估值函数:F=G+H。
  • G:起点到该点的实际耗费。
  • H:该点到终点的预估耗费。
  • F:该点的总耗费。
  • 父亲点:用来保存路径。

算法描述

计算各点的H 
do
{
	寻找open_list中F值最小的点
	将该点加入至close_list 
	for(遍历该点周围的点v)
	{
		if(v在close_list中||v不可通过)
		do nothing
		if(v不在open_list中)
		{
			加入open_list 
			更新该点的G,F,和父亲点 
		}
		else if(v已经在open_list中)
		{
			重新计算该点的G
			G小则更新G,F和父亲点 
		}
	} 
}while(目标点不在open_list&&open_list中还有点)

k短路

  • 第一次找到目标点不停止,第k此找到目标点就是k短路。
  • A*更像是在贪心的基础上附加了对未来的预估的Dijkstra。
  • [USACO08MAR]Cow Jogging G

Code

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1005,maxm=1e4+5;
int n,m,k,cnt,last[maxn],d[maxn],close_list[maxn],g[maxn];
int fronts[maxn],cnt1,dz[maxn],ans;
struct edge{
    int v,w,next;
}e[maxm];
struct edge ez[maxm];
struct heapnode{
    int u,d;
    bool operator<(const heapnode &a)const{
        return d>a.d;
    }
};

struct ele{
    int u,f,g;
    bool operator<(const ele &a)const{
        return f>a.f;
    }
};
void add(int u,int v,int w,edge edg[],int &num,int las[])
{
    edg[++num].v=v;
    edg[num].next=las[u];
    edg[num].w=w;
    las[u]=num;
}

void read()
{
    cin>>n>>m>>k;
    for(int i=1,u,v,w;i<=m;i++)
    {
        cin>>u>>v>>w;
        add(v,u,w,e,cnt,last);
        add(u,v,w,ez,cnt1,fronts);
    }
}

void dijkstra()
{
    priority_queue<heapnode>q;
    for(int i=2;i<=n;i++)
        d[i]=0x3f3f3f3f;
    q.push({1,0});
    while(!q.empty())
    {
        heapnode x=q.top();
        q.pop();
        int u=x.u;
        if(x.d!=d[u])
            continue;
        for(int i=last[u];i;i=e[i].next)
        {
            int v=e[i].v,w=e[i].w;
            if(d[u]+w<d[v])
            {
                d[v]=d[u]+w;
                q.push({v,d[v]});
            }
        }
    }
}

void Astar()
{
    dijkstra();
    priority_queue<ele>q;
    q.push({n,d[n],0});
    do{
        ele x=q.top();
        q.pop();
        int u=x.u,f=x.f,g=x.g;
        if(dz[u]==k) continue;
        dz[u]++;
        if(u==1)
        {
                cout<<f<<endl;
                continue;
        }
        for(int i=fronts[u];i;i=ez[i].next)
        {
            int v=ez[i].v,w=ez[i].w;
            q.push({v,d[v]+g+w,g+w});//u,f,h,g
        }

    }while(!q.empty()&&dz[1]<k);
    if(dz[1]!=k)
    {
        for(int i=dz[1]+1;i<=k;i++)
            cout<<-1<<endl;
    }
}
int main()
{
    read();
    Astar();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希表扁豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值