A*算法求K短路模板 POJ 2449
#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的更多相关文章
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- bellman-ford算法求K短路O(n*m),以及判负环O(n*m)
#include<iostream> #include<algorithm> #include<cstring> using namespace std; cons ...
- Dijkstra算法求最短路模板
Dijkstra算法适合求不包含负权路的最短路径,通过点增广.在稠密图中使用优化过的版本速度非常可观.本篇不介绍算法原理.只给出模板,这里给出三种模板,其中最实用的是加上了堆优化的版本 算法原理 or ...
- k短路模板 POJ2449
采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- A*算法的认识与求第K短路模板
现在来了解A*算法是什么 现在来解决A*求K短路问题 在一个有权图中,从起点到终点最短的路径成为最短路,第2短的路成为次短路,第3短的路成为第3短路,依此类推,第k短的路成为第k短路.那么,第k短路怎 ...
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj 2499第K短路模板
第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...
随机推荐
- 【大厂面试04期】讲讲一条MySQL更新语句是怎么执行的?
流程图 这是在网上找到的一张流程图,写的比较好,大家可以先看图,然后看详细阅读下面的各个步骤. 执行流程: 1.连接验证及解析 客户端与MySQL Server建立连接,发送语句给MySQL Serv ...
- hadoop启动后,9000端口无法连接,netstat -tpnl中找不到该端口
已解决: 需要重新格式化hdfs. 1.停止hdfs: 2.删除hdfs的相关文件目录(hdfs-site.xml中配置的存放文件的目录). 3.启动journalnode:sbin/hadoop-d ...
- ZooKeeper 基础入门
什么是ZooKeeper Apache ZooKeeper 是一个开源的实现高可用的分布式协调服务器.ZooKeeper是一种集中式服务,用于维护配置信息,域名服务,提供分布式同步和集群管理.所有这些 ...
- Hadoop之hadoop fs和hdfs dfs、hdfs fs三者区别
适用范围 案例 备注 小记 hadoop fs 使用范围最广,对象:可任何对象 hadoop dfs 只HDFS文件系统相关 hdfs fs 只HDFS文件系统相关(包括与 ...
- 循序渐进VUE+Element 前端应用开发(9)--- 界面语言国际化的处理
我们开发的系统,一般可以不用考虑语言国际化的问题,大多数系统一般是给本国人使用的,而且直接使用中文开发界面会更加迅速 一些,不过框架最好能够支持国际化的处理,以便在需要的时候,可以花点时间来实现多语言 ...
- App接口设计之token的php实现
为了保证移动端和服务端数据传输相对安全,需要对接口进行加密传输. 一.ttoken的设计目的: 因为APP端没有和PC端一样的session机制,所以无法判断用户是否登陆,以及无法保持用户状态,所以 ...
- redis 深入理解redis 主从复制原理
redis 主从复制 master 节点提供数据,也就是写.slave 节点负责读. 不是说master 分支不能读数据,也能只是我们希望将读写进行分离. slave 是不能写数据的,只能处理读请求 ...
- CRC16冗余循环检测计算器-好用。modbus RTU
开始使用 http://cht.nahua.com.tw/index.php?url=http://cht.nahua.com.tw/software/crc16/&key=Modbus,%2 ...
- 跨云厂商部署 k3s 集群
原文链接:https://fuckcloudnative.io/posts/deploy-k3s-cross-public-cloud/ 最近一两年各大云服务商都出了各种福利活动,很多小伙伴薅了一波又 ...
- [转]IP地址和MAV地址——区别和联系
[转载]http://wenda.tianya.cn/question/27f9476d1e86f6b6 一.IP地址 对于IP地址,相信大家都很熟悉,即指使用TCP/IP协议指定给主机的32位地址 ...