这道题我拖了半年,,,终于写出来了

思路:

先反向建边 从终点做一次最短路 —>这是估价函数h(x)

再正常建边,从起点搜一遍 (priority_queue(h(x)+g(x)))

g(x)是已经走过的。。

思路比较简单,,, 但是我总是MLE

原因:写挫了……

刷了三页… …

//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 100200
int n,m,xx[N],yy[N],zz[N],tot,first[1005],next[N],v[N],w[N],s,e,k,h[1005],vis[1005];
void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
struct Node{int now,h,g;}jy;
priority_queue<Node>pq;
bool operator < (Node a,Node b){return a.g+a.h>b.g+b.h;}
void Dijkstra(){
memset(h,0x3f,sizeof(h));
h[e]=0,jy.now=e;
pq.push(jy);
while(!pq.empty()){
Node t=pq.top();pq.pop();
if(!vis[t.now])vis[t.now]=1;
else continue;
for(int i=first[t.now];~i;i=next[i])
if(!vis[v[i]]&&h[v[i]]>h[t.now]+w[i]){
h[v[i]]=h[t.now]+w[i];
jy.now=v[i];jy.g=h[v[i]];
pq.push(jy);
}
}
}
int A_star(){
memset(vis,0,sizeof(vis));
jy.now=s;jy.g=0;jy.h=h[s];
pq.push(jy);
while(!pq.empty()){
Node t=pq.top();pq.pop();
vis[t.now]++;
if(vis[t.now]>k)continue;
if(vis[e]==k)return t.g;
for(int i=first[t.now];~i;i=next[i]){
jy.now=v[i],jy.g=t.g+w[i],jy.h=h[jy.now];
pq.push(jy);
}
}
return -1;
} int main(){
memset(first,-1,sizeof(first));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&xx[i],&yy[i],&zz[i]),add(yy[i],xx[i],zz[i]);
scanf("%d%d%d",&s,&e,&k);
if(s==e)k++;
Dijkstra();
tot=0,memset(first,-1,sizeof(first));
for(int i=1;i<=m;i++)add(xx[i],yy[i],zz[i]);
printf("%d\n",A_star());
}

POJ 2449 第k短路 Dijkstra+A*的更多相关文章

  1. poj 2449 第k短路

    题目链接:http://poj.org/problem?id=2449 #include<cstdio> #include<cstring> #include<iostr ...

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

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

  3. poj 2499第K短路模板

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

  4. poj 2449 第k短路径

    思路: 利用一个估计函数g[i]=dis[i]+len.其中len为队列出来的点当前已经走了的距离.dis[i]为该点到终点的最短路径.这样我们只要将点按g[i]的升序在队列你排序,每次取出最小的g[ ...

  5. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  6. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  7. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  8. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

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

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

随机推荐

  1. UI_UIImagePickerController(读取图片)

    创建图片 #pragma mark - 创建 photoImageView - (void)createphotoImageView { self.photoImageView = [[UIImage ...

  2. NYOJ 815 三角形【海伦公式】

    /* 关键点:海伦公式 解题人:lingnichong 解题时间:2014-10-04 21:48:47 解题体会:海伦公式的使用 */ 三角形 时间限制:1000 ms  |  内存限制:65535 ...

  3. 【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】

    [168-Excel Sheet Column Title(Excell列标题)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a positive in ...

  4. POJ 2318 叉积判断点与直线位置

    TOYS   Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom ...

  5. JSP中动态include与静态include的区别介绍

    转自:https://m.jb51.net/article/43304.htm 动态INCLUDE 用法:<jsp:include page="included.jsp" f ...

  6. sql server 数据库distinct的用法

    Distinct:用来过滤重复记录.往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值.其原因是distinct只有用二重循环查询来解决,而这样对于一个数据量非常大的站来说,无疑是会直 ...

  7. C++之易混淆知识点一-----static详解

    1.const.mutable与volatile的区别:const表明内存被初始化以后,程序将不能对它进行修改.volatile则表明,即使程序代码没有对内存单元进行修改,但是里面的值也可能会发生变化 ...

  8. hdu1045 - 贪心,二分图

    题目链接 左边白方格里放小球,满足同一行.列只有一个(被黑块隔开).问最多放多少个球. -------------------------------------------------------- ...

  9. 联想E490 加M.2固态硬盘 卡在第一画面不动解决办法

    电脑配置:  E490  500G机械硬盘,自己加M.2 NVMe 固态硬盘. 问题:启动时出现 2101:Detection error on SSD1(M.2),  无法识别到机械硬盘 (重新插拨 ...

  10. luogub P4886 快递员(点分治)

    记得是9月月赛题,当时做的时候觉得跟ZJOI2015幻想乡战略游戏那道题很像???,就写了,然后就写挂了... 我们发现假设当前点为根,我们算出\(m\)次询问中最远的\(a\)对点,如果这\(a\) ...