题目链接

学习博客: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. KMP算法详解-彻底清楚了(转载+部分原创)

    引言 KMP算法指的是字符串模式匹配算法,问题是:在主串T中找到第一次出现完整子串P时的起始位置.该算法是三位大牛:D.E.Knuth.J.H.Morris和V.R.Pratt同时发现的,以其名字首字 ...

  2. linux shell 指令 诸如-d, -f, -e之类的判断表达式简介

    一.文件比较运算符 1. e filename 如果 filename存在,则为真 如: [ -e /var/log/syslog ] 2. -d filename 如果 filename为目录,则为 ...

  3. Python 提案

    学习Java 不可不知JSR,学习Python自然也得知道 PEP了 1- PEP简介 PEP是Python增强提案(Python Enhancement Proposal)的缩写.https://w ...

  4. vue 之 .sync 修饰符

    在一些情况下,我们可能会需要对一个 prop (父子组件传递数据的属性) 进行“双向绑定”. 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的 ...

  5. layui form.on('select(xxx)',function(){});绑定失败

    使用layui的form.on绑定select选中事件中, form.on()不执行, 主要原因有 1, select标签中没有写lay_filter属性,用来监听 <select id=&qu ...

  6. 4、爬虫系列之mongodb

    mongodb mongo简介 简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个介于关系数据库和非关系数 ...

  7. Eclipse debug 断点不能调试 ,Eclipse Unable to install breakpoint in 解决办法

    解决:[1]项目工程名 ,右键 --> properties --> java compiler -->class file Generation 位置  Add line numb ...

  8. (转)Spring Boot(六):如何优雅的使用 Mybatis

    http://www.ityouknow.com/springboot/2016/11/06/spring-boot-mybatis.html 这两天启动了一个新项目因为项目组成员一直都使用的是 My ...

  9. python 逻辑判断 循环练习题

    # 1.判断下列列逻辑语句句的True,False.# 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ...

  10. An Introduction To The SQLite C/C++ Interface

    1. Summary The following two objects and eight methods comprise the essential elements of the SQLite ...