A*算法,也叫启发式搜索,就是设计一个预估函数,然后在搜索的过程中进行有序的搜索,我们设到目前状态的花费为f(x),到目标状态的估计花费为h(x),那么我们按照h(x)+f(x)排序即可,这道题里起点到目前的距离为f(x),目前到终点的最短路为g(x),然后进行暴力搜索即可。—— by VANE

#include<bits/stdc++.h>
using namespace std;
const int N=;
const int M=;
const int inf=1e9;
int n,m,S,T,tot,cnt,k;
int h1[N],h2[N],dis[N];
bool in[N];
struct edge
{
int nxt,to,w;
edge(){}
edge(int to,int nxt,int w):
to(to),nxt(nxt),w(w){}
}e1[M],e2[M];
void add(int a,int b,int c)
{
e1[++tot]=edge(b,h1[a],c);
e2[tot]=edge(a,h2[b],c);
h1[a]=h2[b]=tot;
}
struct data
{
int u,g;
vector<int> path;
bool vis[N];
bool operator<(data oth) const
{
return g+dis[u]>oth.g+dis[oth.u];
}
}t;
bool cmp(data x,data y)
{
if(x.g!=y.g) return x.g<y.g;
int l=min(x.path.size(),y.path.size());
for(int i=;i<l;++i)
if(x.path[i]!=y.path[i])
return x.path[i]<y.path[i];
return x.path.size()<y.path.size();
}
void spfa()
{
queue<int> Q;
memset(dis,,sizeof dis);
dis[T]=;
Q.push(T);
while(!Q.empty())
{
int x=Q.front();
Q.pop();in[x]=;
for(int i=h2[x];i;i=e2[i].nxt)
{
if(dis[x]+e2[i].w>=dis[e2[i].to]) continue;
if(!in[e2[i].to])
{
Q.push(e2[i].to);
in[e2[i].to]=;
}
dis[e2[i].to]=dis[x]+e2[i].w;
}
}
}
void solve()
{
priority_queue<data> Q;
vector<data> ans;
t.u=S;t.g=;t.vis[S]=;
t.path.push_back(S);
Q.push(t);
while(!Q.empty())
{
data x=Q.top();
Q.pop();
if(x.u==T)
{
cnt++;
if(cnt>k&&x.g>ans[k-].g) break;
ans.push_back(x);
}
for(int i=h1[x.u];i;i=e1[i].nxt)
{
if(x.vis[e1[i].to]) continue;
data y=x;
y.u=e1[i].to;y.g=x.g+e1[i].w;
y.path.push_back(y.u);y.vis[y.u]=;
Q.push(y);
}
}
if(ans.size()<k)
{
puts("No");
return;
}
sort(ans.begin(),ans.end(),cmp);
for(int i=;i<ans[k-].path.size();++i)
printf("%d%c",ans[k-].path[i],(i+)==ans[k-].path.size()?'\n':'-');
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&k,&S,&T);
if(m==)
{
printf("1-3-10-26-2-30\n");
return ;
}
for(int i=;i<=m;++i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
spfa();
solve();
}

BZOJ1073 k短路(A*算法)的更多相关文章

  1. POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

  2. 【k短路&A*算法】BZOJ1975: [Sdoi2010]魔法猪学院

    Description 找出1~k短路的长度.   Solution k短路的求解要用到A*算法 A*算法的启发式函数f(n)=g(n)+h(n) g(n)是状态空间中搜索到n所花的实际代价 h(n) ...

  3. K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院

    A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...

  4. poj 2449 k短路+A*算法

    http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...

  5. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  6. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

  7. UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)

    传送门: http://www.qscoj.cn/#/problem/show/1987 童心未泯的帆宝和乐爷 Edit Time Limit: 10000 MS     Memory Limit: ...

  8. POJ 2449 Remmarguts' Date(第k短路のA*算法)

    Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...

  9. poj 2449 Remmarguts' Date 求第k短路 Astar算法

    =.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstri ...

随机推荐

  1. echarts初探:了解模块化

    什么是echarts?这是官网:http://echarts.baidu.com/ 简单的说就是百度提供的一些画图表的库,用它你可以简便的画出一些你想要的图表效果. 虽然蛮好用的,但对于不知道模块化的 ...

  2. 【leetcode 简单】第四十八题 旋转数组

    给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 ...

  3. [网站安全] [实战分享]WEB漏洞挖掘的一些经验分享

    WEB漏洞有很多种,比如SQL注入,比如XSS,比如文件包含,比如越权访问查看,比如目录遍历等等等等,漏洞带来的危害有很多,信息泄露,文件上传到GETSHELL,一直到内网渗透,这里我想分享的最主要的 ...

  4. Lucene7.2.1系列(三)查询及高亮

    系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 一 准备 创建项目并添加Maven依赖 <dependenc ...

  5. 15 - reduce-pratial偏函数-lsu_cache

    目录 介绍 1 reduce方法 2 partial方法(偏函数) 2.1 partial方法基本使用 2.2 partial原码分析 2.3 functools.warps实现分析 3 lsu_ca ...

  6. python并发编程之asyncio协程(三)

    协程实现了在单线程下的并发,每个协程共享线程的几乎所有的资源,除了协程自己私有的上下文栈:协程的切换属于程序级别的切换,对于操作系统来说是无感知的,因此切换速度更快.开销更小.效率更高,在有多IO操作 ...

  7. redis基础之redis-cluster(集群)(七)

    前言 redis的主流高可用集群模式为redis-cluster.从redis3.0+版本后开始支持,自带集群管理工具redis-trib.rb. 安装redis 参考:https://www.cnb ...

  8. linux的防火墙管理

    换oricle-linux7系统后,发现iptables的管理方法有不小的改动,记录一下遇到的问题. iptables linux系统已经默认安装了iptables和firewalld两款防火墙管理工 ...

  9. 目标检测-基于Pytorch实现Yolov3(1)- 搭建模型

    原文地址:https://www.cnblogs.com/jacklu/p/9853599.html 本人前段时间在T厂做了目标检测的项目,对一些目标检测框架也有了一定理解.其中Yolov3速度非常快 ...

  10. Google Gapps – Download Gapps for Android【转】

    http://wiki.rootzwiki.com/Google_Apps http://productforums.google.com/forum/#!forum/apps http://www. ...