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

思路:

先反向建边 从终点做一次最短路 —>这是估价函数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. 实战:percona-xtrabackup 2.1.9 for mysql 5.6.19

    ----1.编译安装percona-xtrabackup yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr ...

  2. pip安装selenium时提示Unknown or unsupported command 'install'

    安装流程: 1.安装Python34 2.安装pip 下载setuptoos并安装,然后输入:easy_install pip 然后 配置path:C:\Python34\Scripts 3安装sel ...

  3. javascript 获取指定范围随机数

    <script type="text/javascript"> function GetRandomNum(Min,Max){ var Range = Max - Mi ...

  4. Residual Networks &lt;2015 ICCV, ImageNet 图像分类Top1&gt;

    本文介绍一下2015 ImageNet中分类任务的冠军--MSRA何凯明团队的Residual Networks.实际上.MSRA是今年Imagenet的大赢家.不单在分类任务,MSRA还用resid ...

  5. 1. Git-2.12.0-64-bit .exe下载

    转自:https://blog.csdn.net/u011164906/article/details/59129835 之前一直用SVN最近接触git,Git-2.12.0-64-bit .exe文 ...

  6. ing在写作中到底怎么用

    改关静留的作业中我还得改英文摘要.于是有个地方用了ing.顺便查了一下. http://www.yygrammar.com/Article/201408/3677.html -ing分词用作状语时有九 ...

  7. c#获取DataTable某一列不重复的值,或者获取某一列的所有值

    实现该功能是用了DataView的筛选功能,DataView表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. 这里做了一个简单易懂的Demo来讲述该方法. 1.建 ...

  8. C# 将string 转换为二维码图片,然后转为base64字符串编码 。

    需在nuget 添加此dll ///content字符串 public static string GetQRCode(string content, int moduleSize = 9) { va ...

  9. 简单暴力的TP5多主题方案

    一个小项目,需要配置多套前端主题.解决的思路是根据域名加载不同的主题配置. 一.在应用目录 application 下创建 common 目录. 二.application/common 目录下创建 ...

  10. 计算a-b的差[返回BigDecimal 类型]

    /*** * 返回 a-b 的差 [返回 BigDecimal 类型] * @param a 被减数 * @param b 减数 * @return */ public static BigDecim ...