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*[ ...
随机推荐
- Android-Universal-Image-Loader学习笔记(3)--内存缓存
前面的两篇博客写了文件缓存.如今说说Android-Universal-Image-Loader的内存缓存.该内存缓存涉及到的类如图所看到的 这些类的继承关系例如以下图所看到的: 如同文件缓存一样,内 ...
- myeclipse配置内存
1.javaee项目假设耗费的内存过大,须要配置内存大小: 下图是配置tomcat结果:Optional program arguments: -Xms512M -Xmx512M -XX:PermSi ...
- 一些.NET 项目中经常使用的类库
Web自己主动化測试 Watin Selenium Selenium git .net 集合类扩展实现C5 Subscriber/Publisher 模式 Rx Nats 防御式编程 断言库 流 ...
- IntelliJ IDEA中JAVA连接MYSQL
1.下载mysql包 2.项目中引入mysql包 3.连接数据库,查询结果 看jdbc数据库连接类 package Facade; import java.sql.*; /** * Created b ...
- javascript系列-class6.String类型
观察淘宝网商品数据 有一个东西叫服务器>>>>js的作用重要作用之一>>>>交互>>>>人机交互(事件)>>&g ...
- 当一个元素被浮动后,它的display是否会被默认指定为block?
css 浮动后的元素不论是什么display的都默认是block就是设置inline也是block IE/6出现双边框的原因 出现双边距的条件是当浮动元素的浮动方向和margin的方向一致时才会出现. ...
- Linux部署之批量自动安装系统之DHCP篇
1. 安装:yum install dhcp 2. Ip配置信息 3. Dhcp配置文件如下 4. 配置完后检查语法是否错误 ...
- POJ 3660 Cow Contest【传递闭包】
解题思路:给出n头牛,和这n头牛之间的m场比赛结果,问最后能知道多少头牛的排名. 首先考虑排名怎么想,如果知道一头牛打败了a头牛,以及b头牛打赢了这头牛,那么当且仅当a+b+1=n时可以知道排名,即为 ...
- 通过curl获取网页访问时间
curl -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download}&quo ...
- nginx的location 匹配的规则问题
正则解释: ~ #匹配一个正则匹配,区分大小写~* #匹配一个正则,不区分大小写^~ #普通字符匹配,如果该选择匹配不匹配别的选项,一般用来匹配目录= #精确匹配 匹配案例:location = / ...