BZOJ1073 k短路(A*算法)
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*算法)的更多相关文章
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- 【k短路&A*算法】BZOJ1975: [Sdoi2010]魔法猪学院
Description 找出1~k短路的长度. Solution k短路的求解要用到A*算法 A*算法的启发式函数f(n)=g(n)+h(n) g(n)是状态空间中搜索到n所花的实际代价 h(n) ...
- K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院
A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...
- poj 2449 k短路+A*算法
http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)
传送门: http://www.qscoj.cn/#/problem/show/1987 童心未泯的帆宝和乐爷 Edit Time Limit: 10000 MS Memory Limit: ...
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- poj 2449 Remmarguts' Date 求第k短路 Astar算法
=.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstri ...
随机推荐
- hihocoder1415 后缀数组三·重复旋律3
传送门:http://hihocoder.com/problemset/problem/1415 [题解] 考虑求出两串合在一起(中间加分隔符)后缀数组,就是要求任意在两个串中的$i, j$,$\mi ...
- cocos2dx学习,转摘一些链接
cocos2d-x学习笔记09:动作2:持续动作 ccBezierConfig 贝塞尔坐标点是相对的 Box2DTestBed很有意思的demo,可惜自己水平有限针对其实现还是没弄明白,以后有时间多学 ...
- 大聊Python----迭代器
迭代器 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的ge ...
- Java源码-HashMap(jdk1.8)
一.hash方法 如下是jdk1.8中的源码 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = ke ...
- flask插件系列之flask_celery异步任务神器
现在继续学习在集成的框架中如何使用celery. 在Flask中使用celery 在Flask中集成celery需要做到两点: 创建celery的实例对象的名字必须是flask应用程序app的名字,否 ...
- 离线下载pip包进行安装【转】
Host-A 不能上网,但是需要在上面安装Python-package 通过另外一台能上网的Host-B主机 1. 下载需要离线安装的Packages 在Host-B上执行如下命令: 安装单个Pack ...
- Appium 1.6.3使用的自动化测试引擎
automationName项的值: Appium:默认值. Selendroid:安卓2.3(API 9)-4.1(API 16)版本使用. UiAutomator2:最新安卓版本. XCUITes ...
- [转载]锁无关的数据结构与Hazard指针——操纵有限的资源
Lock-Free Data Structures with Hazard Pointers 锁无关的数据结构与Hazard指针----操纵有限的资源 By Andrei Alexandrescu a ...
- Vim的分屏功能
本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 f ...
- C++——stoi函数
版权声明:本文系原创,转载请声明出处. 1. 函数原型 , ); , ); 2. 参数说明 str String object with the representation of an integr ...