POJ 2449 第k短路 Dijkstra+A*
这道题我拖了半年,,,终于写出来了
思路:
先反向建边 从终点做一次最短路 —>这是估价函数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*的更多相关文章
- poj 2449 第k短路
题目链接:http://poj.org/problem?id=2449 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- poj 2499第K短路模板
第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...
- poj 2449 第k短路径
思路: 利用一个估计函数g[i]=dis[i]+len.其中len为队列出来的点当前已经走了的距离.dis[i]为该点到终点的最短路径.这样我们只要将点按g[i]的升序在队列你排序,每次取出最小的g[ ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- poj 2449(A*求第K短路)
题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...
随机推荐
- 应用Spring和Hibernate(C3P0数据池)写数据库交互项目
一.xml的配置文件(application.xml) <?xml version="1.0" encoding="UTF-8"?> <bea ...
- 对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)
IFS: 对IFS的用处直接进行说明,详细IFS是干什么的...自行谷歌 首先创建一个 "a a",和"a"的文件: 然后我们 ls查看一下: --> l ...
- UVALive 4222 /HDU 2961 Dance 大模拟
Dance Problem Description For a dance to be proper in the Altered Culture of Machinema, it must abid ...
- .net中的目录
System.Environment.CurrentDirectory Application.StartupPath https://msdn.microsoft.com/en-us/library ...
- CentOS 6.5 下编译安装 Nginx 1.8.0
转自:https://i.cnblogs.com/EditPosts.aspx?postid=8303227&update=1 安装编译依赖的包 yum -y install gcc gcc- ...
- Visual Studio 2015 官方下载及密钥
Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品.Visual Studio 2015 是一个丰富的集成开发环境,可用于创建出色的 Windows.Andr ...
- select2多选
在TCX_1710项目中的拒绝代码配置页面可以选择多个拒绝字段,效果图如下 代码中需要注意的有以下地方:图1为提交保存时对多选数据的获取,图2为修改是对多选数据的回显 对于多选框宽度太窄的问题,可以参 ...
- 关于github里readme编辑的方法
实验室的老师昨天改完论文发我后,说按照例子改.于是才发现github里readme编辑满满的极客思维. 看了一下csdn给的教程 https://blog.csdn.net/Kaitiren/arti ...
- 安卓、safari和微信各个浏览器的设计标准
- NOIp2018模拟赛四十一
现在是下午15:36,我已经改完题了...刷新纪录 A题叫B,B题叫A是什么操作??? 我离200只差一个SBFA.jpg 成绩:0+100+10=110 被虐啦...今天九个阿克的Orz.A题是个神 ...