#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=2e6+5;
struct asd{
int from,to,next,val;
}b[maxn],b2[maxn];
int head[maxn],tot=1;
int h2[maxn],t2=1;
int n,m;
void ad(int aa,int bb,int cc){
b[tot].from=aa;
b[tot].to=bb;
b[tot].val=cc;
b[tot].next=head[aa];
head[aa]=tot++;
}
void ad2(int aa,int bb,int cc){
b2[t2].from=aa;
b2[t2].to=bb;
b2[t2].val=cc;
b2[t2].next=h2[aa];
h2[aa]=t2++;
}
struct jie{
int num,dis;
jie(int aa=0,int bb=0){
num=aa,dis=bb;
}
bool operator < (const jie& A) const{
return dis>A.dis;
}
};
priority_queue<jie> q;
int dis[maxn];
bool vis[maxn];
void DIJ(int xx){
dis[xx]=0;
q.push(jie(xx,0));
while(!q.empty()){
int now=q.top().num;
q.pop();
if(vis[now]) continue;
vis[now]=1;
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
if(dis[u]>dis[now]+b[i].val){
dis[u]=dis[now]+b[i].val;
q.push(jie(u,dis[u]));
}
}
}
}
struct as{
int qd,hx,gx;
as(int aa=0,int bb=0,int cc=0){
qd=aa,hx=bb,gx=cc;
}
bool operator < (const as& A) const{
return hx+gx>A.hx+A.gx;
}
};
int cnt[maxn];
priority_queue<as> qq;
int Astar(int s,int t,int k){
if(dis[s]==0x3f3f3f3f) return -1;
qq.push(as(s,dis[s],0));
while(!qq.empty()){
int nqd=qq.top().qd,nhx=qq.top().hx,ngx=qq.top().gx;
qq.pop();
cnt[nqd]++;
if(cnt[nqd]==k && nqd==t) return ngx;
if(cnt[nqd]>k) continue;
for(int i=h2[nqd];i!=-1;i=b2[i].next){
int u=b2[i].to;
qq.push(as(u,dis[u],ngx+b2[i].val));
}
}
return -1;
}
int main(){
memset(dis,0x3f,sizeof(dis));
memset(head,-1,sizeof(head));
memset(h2,-1,sizeof(h2));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
ad2(aa,bb,cc);
ad(bb,aa,cc);
}
int s,t,k;
scanf("%d%d%d",&s,&t,&k);
if(s==t) ++k;
DIJ(t);
printf("%d\n",Astar(s,t,k));
return 0;
}

A*算法求K短路模板 POJ 2449的更多相关文章

  1. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  2. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

  3. bellman-ford算法求K短路O(n*m),以及判负环O(n*m)

    #include<iostream> #include<algorithm> #include<cstring> using namespace std; cons ...

  4. Dijkstra算法求最短路模板

    Dijkstra算法适合求不包含负权路的最短路径,通过点增广.在稠密图中使用优化过的版本速度非常可观.本篇不介绍算法原理.只给出模板,这里给出三种模板,其中最实用的是加上了堆优化的版本 算法原理 or ...

  5. k短路模板 POJ2449

    采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...

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

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

  7. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  8. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  9. poj 2499第K短路模板

    第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...

随机推荐

  1. Python 网络爬虫基本概念篇

    爬虫的概念 网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.这是百度百科对爬虫的定义,其实,说简单点,爬虫 ...

  2. [OpenGL](翻译+补充)投影矩阵的推导

    1.简介 基本是翻译和补充 http://www.songho.ca/opengl/gl_projectionmatrix.html 计算机显示器是一个2D的平面,一个3D的场景要被OpenGL渲染必 ...

  3. 在已经编译安装好php7场景下,install gd库 with free-type (解决Call to undefined function imagettftext())

    在已经编译安装好php7场景下,install gd库 with free-type (解决Call to undefined function   imagettftext()) install g ...

  4. 如何在Linux上安装Redis(内附详细教程)

    前言 hello,好久不见,又断更了一段时间.同事大部分离职了,但是活还是一样,所以只能硬着头皮顶上.现在总算歇会了,决定开启Redis源码系列,希望不要啪啪啪打脸. ​ 什么是redis? Redi ...

  5. 别在重复造轮子了,几个值得应用到项目中的 Java 开源库送给你

    我是风筝,公众号「古时的风筝」.文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面.公众号回复『666』获取高清大图. 风筝我作为一个野路子开发者,直到 ...

  6. Postsharp简单试用——在业务逻辑类上添加日志记录

    1.首先添加PostSharp引用 2.添加特性(Attribute)类 [Serializable] [AttributeUsage(AttributeTargets.Method, AllowMu ...

  7. springboot整合Mybatis(有xml)

    pom.xml <!-- mybatis 支持 SpringBoot --> <dependency> <groupId>org.mybatis.spring.bo ...

  8. ZWave 数据包格式

    ZWave Device 的过程中,其实就是对不同的 COMMAND CLASS 进行解析处理.在刚开始进入ZWAVE 开发时,为了弄清楚数据包的格式,做了下面的参考表格.不过后来发现用来抓包的 sn ...

  9. Eplan如何添加“连接定义点”

    Eplan如何添加“连接定义点” 参考文档:https://blog.csdn.net/txwtech/article/details/90510106

  10. 能被 K 整除的最大连续子串长度

    [来源]网上流传的2017美团秋招笔试题 [问题描述] 两个测试样例输出都是5 [算法思路] 暴力解法时间会超限,使用一种很巧妙的数学方法.用在读取数组arr时用数组sum记录其前 i 项的和,即 s ...