题目链接

学习博客:https://blog.csdn.net/Z_Mendez/article/details/47057461

k短路没有我想象的那么难,还是很容易理解的

求s点到t点的第k短路径

先求出t到所有点的最短路径,用g[i]表示t到i的距离

从s开始”bfs“,按照(g[i]+bfs路过的长度)构造优先队列,比如刚开始bfs路过长度为0,所在点为s

一直选择最小的(g[i]+bfs路过的长度),第一次到达t一定是从s沿着最短路径到达。

直到第k次到达t

理解代码可能更容易些

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define ll long long
const int maxn=1e3+5;
const int INF=1e9; struct line
{
int to,w;
line(int _to,int _w)
{
to=_to,w=_w;
}
};
vector<line>ma1[maxn],ma2[maxn];//ma1为真实的路线,ma2为逆的,为了计算终点到各个点的最短距离
int g[maxn],vis[maxn];//g数组为终点到各个点的最短距离
int n,m,s,t,k,time;
struct node
{
int x,time;//当前节点走过的距离time + x到终点的距离就是这条路线的距离
node(int _x,int _time)
{
x=_x,time=_time;
}
bool operator<(const node &a)const
{
return g[x]+time>g[a.x]+a.time;
}
};
void dij()//为了构造g数组
{
for(int i=1;i<=n;i++)vis[i]=0,g[i]=1e9;
g[t]=0;
for(int i=1;i<=n;i++)
{
int inde,mi=1e9+10;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&mi>g[j])
{
mi=g[j];
inde=j;
}
}
vis[inde]=1;
for(int j=0;j<ma2[inde].size();j++)
g[ma2[inde][j].to]=min(g[ma2[inde][j].to],g[inde]+ma2[inde][j].w);
}
}
int astar()//根据g数组指引“bfs”运动
{
priority_queue<node>que;
que.push(node(s,0));
if(s==t)
k++;
while(que.size())
{
node now=que.top();
que.pop();
if(now.x==t)time++; //到达终点
if(time==k)return now.time;
for(int i=0;i<ma1[now.x].size();i++)
{
que.push(node(ma1[now.x][i].to,now.time+ma1[now.x][i].w));
}
}
return -1;
}
int main()
{ cin>>n>>m;
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
ma1[a].push_back(line(b,c));
ma2[b].push_back(line(a,c));
}
cin>>s>>t>>k;
dij();
cout<<astar()<<endl;;
return 0;
}

  

poj2449 第k短路的更多相关文章

  1. poj2449第K短路问题(A*算法)

    启发函数:f(x)=g(x)+h(x); g(x)表示初始点到x状态的代价,h(x)表示从x的状态到目标状态的代价的估计值(并不是真实的),实际最小代价<=h(x); 起点s,终点t,x.v=s ...

  2. POJ2449 (k短路)

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  3. POJ2449 Remmarguts' Date 第K短路

    POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...

  4. A*模板(求K短路)(POJ2449)

    A*是bfs的优化,IDA*是dfs的优化 A*算法: 为启发式算法中很重要的一种,被广泛应用在最优路径求解和一些策略设计的问题中.而A*算法最为核心的部分,就在于它的一个估值函数的设计上: f(n) ...

  5. k短路模板 POJ2449

    采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  6. 第K短路模板【POJ2449 / 洛谷2483 / BZOJ1975 / HDU6181】

    1.到底如何求k短路的? 我们考虑,要求k短路,要先求出最短路/次短路/第三短路……/第(k-1)短路,然后访问到第k短路. 接下来的方法就是如此操作的. 2.f(x)的意义? 我们得到的f(x)更小 ...

  7. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  8. POJ2449:K短路

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 26355   Accepted: 7170 ...

  9. poj2449(k短路&A_star模板)

    题目链接:http://poj.org/problem?id=2449 题意:给出一个有向图,求s到t的第k短路: 思路:k短路模板题,可以用A_star模板过: 单源点最短路径+高级搜索A*;A*算 ...

随机推荐

  1. 初学ubuntu之文件权限权限

    今天接着做笔记,坚持学习下去. 文件权限修改命令,初学者看见这个命令之后总有些摸不着头脑,这命令里面用到了一些数字,我 自己也是,这次写一篇自己的认识.希望能够帮助到需要学习的人. 首先你可以通过 l ...

  2. JSON Web Tokens简单学习

    JWT用于加密生成安全认证的令牌,存储登录验证成功的部分用户信息 一.安装JWT 二.加密 解密 代码 /*存储在加密字符串的信息*/ var payload = new Dictionary< ...

  3. 关于svn上传.classpath等问题

    1. svn版本:客户端版本与服务器版本 要尽量适配. 2. svn管理项目:有人将.classpath, .project, .mymetadata, .myumldata等文件也纳入到版本控制,如 ...

  4. windows 解放鼠标快捷键

    win+ 调整某个窗口的放大缩小靠边站,最小化 窗口间的切换alt+tablealt (按住)+table(一下)+ 上下左右 alt(一下)+table(一下)相邻切换 alt(按住)+tables ...

  5. mysql 数据可视化操作---Navicat安装及简单使用

    ,一.安装 下载地址:https://pan.baidu.com/s/1bpo5mqj 安装方法:https://www.cnblogs.com/clschao/articles/10022040.h ...

  6. postgresql 按日期范围查询

    Timestamp without timezone 方法一: select * from user_info where create_date >= '2015-07-01' and cre ...

  7. A. On The Way to Lucky Plaza 概率 乘法逆元

    A. On The Way to Lucky Plaza time limit per test 1.0 s memory limit per test 256 MB input standard i ...

  8. 寒假特训——搜索——H - Nephren gives a riddle

    What are you doing at the end of the world? Are you busy? Will you save us? Nephren is playing a gam ...

  9. 【gdoi2018 day2】第二题 滑稽子图

    题意: 给出一棵树.设\(E\)表示边集,\(V\)表示点集,\(S\)为\(V\)的一个子集. \(f(S)=|(u,v)|(u,v)\in E \ \&\&\ u\in V\ \& ...

  10. 一入python深似海--class

    python class 分为三个部分:class and object(类与对象),inheritance(继承),overload(重载)and override(覆写). class and o ...