A* 算法求第 K 短路
一种具有 \(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 短路的更多相关文章
- aStar算法求第k短路
A*的概念主意在于估计函数,f(n)=g(n)+h(n),f(n)是估计函数,g(n)是n节点的当前代价,h(n)是n节点的估计代价:而实际中,存在最优的估计函数f'(n)=g'(n)+h'(n),那 ...
- A*算法的认识与求第K短路模板
现在来了解A*算法是什么 现在来解决A*求K短路问题 在一个有权图中,从起点到终点最短的路径成为最短路,第2短的路成为次短路,第3短的路成为第3短路,依此类推,第k短的路成为第k短路.那么,第k短路怎 ...
- poj 2449(A*求第K短路)
题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...
- POJ 2499 A*求第K短路
DES就是给你一个图.然后给你起点和终点.问你从起点到终点的第K短路. 第一次接触A*算法. 题目链接:Remmarguts' Date 转载:http://blog.csdn.net/mbxc816 ...
- A* 算法求第k短路径
A*算法是一类贪心算法,其可以用于寻找最优路径.我们可以利用A*算法来求第k短路径. 一条路径可以由两部分组成,第一部分是一个从出发到达任意点的任意路径,而第二部分是从第一部分的末端出发,到终点的最短 ...
- POJ 2449 求第K短路
第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...
- poj 2449 Remmarguts' Date 求第k短路 Astar算法
=.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstri ...
- ACM模板~求第k短路 ~~~A*+Dijkstra
#include <map> #include <set> #include <cmath> #include <ctime> #include < ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
随机推荐
- 如何将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
cat /etc/issue|tr '[:lower:]' [:upper:] >> /tmp/issue.out
- sqlplus无法登陆?
关键词:error 6 initialize sqlplus,ORA-27101: shared memory realm does not exist 1.error 6 initialize sq ...
- mysql自动生成my.cnf文件
http://imysql.com/my-cnf-wizard.html
- 只使用非递归的mutex
mutex分为递归(以下简写为rm)和非递归(以下简写为nrm)两种,它们的唯一区别在于:同一个线程可以重复对rm加锁,但是不能重复对nrm加锁. 虽然rm使用起来要更加方便一些,并且不用考虑一个线程 ...
- js中箭头函数 及 针对箭头函数this指向问题引出的单体模式
ES6允许使用“箭头”(=>)定义函数 var f = a = > a //等同于 var f = function(a){ return a; } 如果箭头函数不需要参数或需要多个参数, ...
- 搜索专题: HDU1016Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [VINS]IMU与相机之间旋转量的标定
VINS-Mono[1]中IMU-Camera外参旋转量\(R_b^c\)的计算方法在他们实验室发的之前的论文有详细讲解[2].视觉利用匹配特征点中的基础矩阵求出相机坐标系下两帧的旋转量\(R_{c_ ...
- NSPredicate的使用,超级强大
NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...]; Format: (1)比较运算符>,<,==, ...
- C++ STL(标准模板库)
一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的,在被引入C++之前该技术就已经存在了很长的一段时间. STL的代码从广义上讲分为三类:alg ...
- 可持久化+Trie || BZOJ 3261最大异或和 || Luogu P4735 最大异或和
题面:最大异或和 代码: #include<cstdio> #include<cstring> #include<iostream> using namespace ...