启发函数:f(x)=g(x)+h(x);

g(x)表示初始点到x状态的代价,h(x)表示从x的状态到目标状态的代价的估计值(并不是真实的),实际最小代价<=h(x);

起点s,终点t,x.v=s,x.len=0;然后优先队列中f(x)值最小值出队列,再根据出队列的x.v状态发展下一层。如果出队列时第一次遇到x.v==t,

就找到了s到t的最短路。...如果第k次,那就是第k短。为了加速计算,h(p)需要在A*搜索之前进行预处理,只要将原图的所有边反向,

再从终点t做一次单源点最短路径就能得到每个点的h(p)了;

其实到现在可以发现,如果g(x)为0,那么求出来的就是最短路系列,如果h(x)为0,求出来的就是BFS最少次数。

#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 99999999
using namespace std;
const int maxn = ;
struct Enode
{
int to;
int val;
int next;
}edge1[],edge2[];
struct node
{
int to;
int g,f;//g别的点到此状态的代价 f启发函数
friend bool operator<(node a,node b){
if(a.f!=b.f)
return a.f>b.f;
return a.g>a.g;
}
};
int n,dis[maxn],pre1[maxn],pre2[maxn],index1,index2;
void init()
{
index1=index2=;
memset(pre1,-,sizeof(pre1));
memset(pre2,-,sizeof(pre2));
}
void add1(int x,int y,int z)
{
edge1[index1].to=y;
edge1[index1].val=z;
edge1[index1].next=pre1[x];
pre1[x]=index1++;
}
void add2(int x,int y,int z)
{
edge2[index2].to=y;
edge2[index2].val=z;
edge2[index2].next=pre2[x];
pre2[x]=index2++;
}
void spfa(int s)
{
queue<int>q;
int vis[maxn],i,j;
for(i=;i<=n;i++)
{
vis[i]=;
dis[i]=INF;
}
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty())
{
int t=q.front();
q.pop();
vis[t]--;
if(vis[t]>n)
break;
for(i=pre2[t];i!=-;i=edge2[i].next)
{
if(dis[edge2[i].to]>dis[t]+edge2[i].val)
{
dis[edge2[i].to]=dis[t]+edge2[i].val;
q.push(edge2[i].to);
}
}
}
}
int A_star(int s,int t,int k)
{
node temp;
priority_queue<node>q;
int cnt=;
if(s==t) k++;
if(dis[s]==INF)
return -;
temp.to=s;
temp.g=;
temp.f=temp.g+dis[temp.to];
q.push(temp);
while(!q.empty())
{
temp=q.top();
q.pop();
if(temp.to==t)
{
cnt++;
}
if(cnt==k)
{
return temp.g;
}
for(int i=pre1[temp.to];i!=-;i=edge1[i].next)
{
node tt;
tt.to=edge1[i].to;
tt.g=temp.g+edge1[i].val;
tt.f=tt.g+dis[tt.to];
q.push(tt);
}
}
return -;
}
int main()
{
int i,j,m;
while(~scanf("%d%d",&n,&m))
{
init();
for(i=;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add1(x,y,z);//原图
add2(y,x,z);//反向图
}
int s,t,k;
scanf("%d%d%d",&s,&t,&k);
spfa(t);
int ans=A_star(s,t,k);
printf("%d\n",ans);
}
}

poj2449第K短路问题(A*算法)的更多相关文章

  1. ACM-ICPC 2018 沈阳赛区网络预赛 D Made In Heaven(第k短路,A*算法)

    https://nanti.jisuanke.com/t/31445 题意 能否在t时间内把第k短路走完. 分析 A*算法板子. #include <iostream> #include ...

  2. poj2449 第k短路

    题目链接 学习博客:https://blog.csdn.net/Z_Mendez/article/details/47057461 k短路没有我想象的那么难,还是很容易理解的 求s点到t点的第k短路径 ...

  3. BZOJ1073 k短路(A*算法)

    A*算法,也叫启发式搜索,就是设计一个预估函数,然后在搜索的过程中进行有序的搜索,我们设到目前状态的花费为f(x),到目标状态的估计花费为h(x),那么我们按照h(x)+f(x)排序即可,这道题里起点 ...

  4. POJ2449 (k短路)

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  5. A*模板(求K短路)(POJ2449)

    A*是bfs的优化,IDA*是dfs的优化 A*算法: 为启发式算法中很重要的一种,被广泛应用在最优路径求解和一些策略设计的问题中.而A*算法最为核心的部分,就在于它的一个估值函数的设计上: f(n) ...

  6. K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院

    A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...

  7. poj 2449 k短路+A*算法

    http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...

  8. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  9. POJ2449:K短路

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 26355   Accepted: 7170 ...

随机推荐

  1. ROS urdf和xacro文件详解

    视觉标签:visual <visual> <origin xyz="0.0 0.0 0.0" /> <geometry> <box siz ...

  2. 吴恩达《机器学习》课程总结(5)_logistic回归

    Q1分类问题 回归问题的输出可能是很大的数,而在分类问题中,比如二分类,希望输出的值是0或1,如何将回归输出的值转换成分类的输出0,1成为关键.注意logistics回归又称 逻辑回归,但他是分类问题 ...

  3. vue 路由入门(vue-router)

    新建的 js 文件如下: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //全局使用该组件 / ...

  4. 原生JS实现彩票36选7不重复(优化)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. PAT甲级——A1033 To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  6. 隐藏和显示<td>

    <td id="ifXc"><input type="text" value="1"></td>隐藏$( ...

  7. Eureka客户端无法连接服务注册中心

    转载自:https://my.oschina.net/kousm/blog/2249003 服务端 application.yml配置 spring: application: name: eurek ...

  8. WPF 自适应布局控件

    public class KDLayoutGroup : Grid { public double LabelWidth { get; set; } public double GetLabelWid ...

  9. hbase 利用rowkey设计进行多条件查询

    摘要 本文主要内容是通过合理Hbase 行键(rowkey)设计实现快速的多条件查询,所采用的方法将所有要用于查询中的列经过一些处理后存储在rowkey中,查询时通过rowkey进行查询,提高rowk ...

  10. WCF简要介绍

    什么是WCF WCF的全称是:Windows Communication Foundation.从本质上来说,它是一套软件开发包,是微软公司推出的符合SOA思想的技术框架.WCF为程序员提供了丰富的功 ...