一种具有 \(f(n)=g(n)+h(n)\) 策略的启发式算法能成为 A* 算法的充分条件是:

  • 搜索树上存在着从起始点到终了点的最优路径。
  • 问题域是有限的。
  • 所有结点的子结点的搜索代价值 \(>0\)。
  • \(h(n) \le h^\ast (n)\) (\(h^\ast (n)\) 为实际问题的代价值)。

Remmarguts' Date

求 S 到 T 的第 K 短路。

思路:

(1) 将有向图的所有边反向,以原终点T为源点,求解T到所有点的最短距离。

(2) 新建一个优先队列,将源点S加入到队列中。

(3) 从优先级队列中弹出f(p)最小的点p,如果点p就是T,则计算T出队的次数。如果当前为T的第K次出队,则当前路径的长度就是S到T的第K短路的长度,算法结束;否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先队列。

Z_Mendez

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; int n, m, S, T, K, ans=-1;
int head[1005], nex[100005], to[100005], w[100005];
int rhead[1005], rnex[100005], rto[100005], rw[100005];
int h[1005]; bool v[1005];
struct node {
int t, d;
bool operator < (const node& A) const {return d>A.d; }
};
priority_queue<node> q;
struct star {
int t, g, f;
bool operator < (const star& A) const {return f>A.f || f==A.f && g>A.g; }
};
priority_queue<star> Q; inline void add(int x, int y, int z) {
nex[++head[0]]=head[x], head[x]=head[0], to[head[0]]=y, w[head[0]]=z;
}
inline void radd(int x, int y, int z) {
rnex[++rhead[0]]=rhead[x], rhead[x]=rhead[0], rto[rhead[0]]=y, rw[rhead[0]]=z;
} inline void astar() {
if (S==T) ++K;
if (h[S]==0x3f3f3f3f) return;
Q.push((star) {S, 0, h[S]});
register int cnt=0; while (!Q.empty()) {
register star now=Q.top(); Q.pop();
if (now.t==T) if (++cnt>=K) {ans=now.g; return; }
for (int i=head[now.t]; i; i=nex[i])
Q.push((star) {to[i], now.g+w[i], now.g+w[i]+h[to[i]]});
}
} int main() {
while (~scanf("%d%d", &n, &m)) {
memset(head, 0, sizeof head), memset(rhead, 0, sizeof rhead);
for (int i=1, A, B, C; i<=m; ++i) scanf("%d%d%d", &A, &B, &C),
add(A, B, C), radd(B, A, C);
scanf("%d%d%d", &S, &T, &K); memset(h, 0x3f, sizeof h);
q.push((node) {T, h[T]=0});
while (!q.empty()) {
register node now=q.top(); q.pop();
if (v[now.t]) continue; v[now.t]=true;
for (int i=rhead[now.t]; i; i=rnex[i])
if (now.d+rw[i] < h[rto[i]])
h[rto[i]] = now.d + rw[i], q.push((node) {rto[i], h[rto[i]]});
} astar();
printf("%d\n", ans);
}
return 0;
}

A* 算法求第 K 短路的更多相关文章

  1. aStar算法求第k短路

    A*的概念主意在于估计函数,f(n)=g(n)+h(n),f(n)是估计函数,g(n)是n节点的当前代价,h(n)是n节点的估计代价:而实际中,存在最优的估计函数f'(n)=g'(n)+h'(n),那 ...

  2. A*算法的认识与求第K短路模板

    现在来了解A*算法是什么 现在来解决A*求K短路问题 在一个有权图中,从起点到终点最短的路径成为最短路,第2短的路成为次短路,第3短的路成为第3短路,依此类推,第k短的路成为第k短路.那么,第k短路怎 ...

  3. poj 2449(A*求第K短路)

    题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...

  4. POJ 2499 A*求第K短路

    DES就是给你一个图.然后给你起点和终点.问你从起点到终点的第K短路. 第一次接触A*算法. 题目链接:Remmarguts' Date 转载:http://blog.csdn.net/mbxc816 ...

  5. A* 算法求第k短路径

    A*算法是一类贪心算法,其可以用于寻找最优路径.我们可以利用A*算法来求第k短路径. 一条路径可以由两部分组成,第一部分是一个从出发到达任意点的任意路径,而第二部分是从第一部分的末端出发,到终点的最短 ...

  6. POJ 2449 求第K短路

    第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...

  7. poj 2449 Remmarguts' Date 求第k短路 Astar算法

    =.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstri ...

  8. ACM模板~求第k短路 ~~~A*+Dijkstra

    #include <map> #include <set> #include <cmath> #include <ctime> #include < ...

  9. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

随机推荐

  1. [转帖]深入理解 MySQL—锁、事务与并发控制

    深入理解 MySQL—锁.事务与并发控制 http://www.itpub.net/2019/04/28/1723/ 跟oracle也类似 其实所有的数据库都有相同的机制.. 学习了机制才能够更好的工 ...

  2. The order of a Tree

    The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...

  3. Python 实用脚本

    Python 实用脚本 脚本写的好,下班下得早!程序员的日常工作除了编写程序代码,还不可避免地需要处理相关的测试和验证工作. 例如,访问某个网站一直不通,需要确定此地址是否可访问,服务器返回什么,进而 ...

  4. 二、JVM — 垃圾回收

    JVM 垃圾回收 写在前面 本节常见面试题 本文导火索 1 揭开 JVM 内存分配与回收的神秘面纱 1.1 对象优先在 eden 区分配 1.2 大对象直接进入老年代 1.3 长期存活的对象将进入老年 ...

  5. jQuery之链式编程

    使用的思想:隐式迭代. <button>快速</button> <button>快速</button> <button>快速</but ...

  6. git error: failed to push some refs to 'git@github.com:xxx/xxx.git'

    本地仓库中和远程仓库不一致,缺少readme.md文件 解决方式参见:https://blog.csdn.net/qq_37281252/article/details/79044798

  7. k3 cloud的单据存储在业务对象表中

    k3 cloud的单据存储在业务对象表中,表名为T_META_OBJECTTYPE,查询表名和对应的表: select FNAME,FBASEOBJECTID from T_META_OBJECTTY ...

  8. vue项目1-pizza点餐系统9-axios实现数据存储

    一.安装.引入axios 1.终端输入cnpm install axios 2.在main.js中引入 import axios from ‘axios’ 3.配置路径 axios.defaults. ...

  9. <img> 标签的 src 属性

    src属性 加载的时候就会请求 1.servlet生成一个图片 2.你直接输入servlet的连接看一下,就是一个图片,和我们自己发布到服务器的一样. 3.页面加载时,会访问这个servelt连接,自 ...

  10. UITextView学习笔记

    =================================== UITextView =================================== 1.UITextView常用属性 ...