题目链接:http://poj.org/problem?id=2449

题意:给出一个有向图,求s到t的第k短路;

思路:k短路模板题,可以用A_star模板过;

单源点最短路径+高级搜索A*;
A*算法结合了启发式方法和形式化方法;
启发式方法通过充分利用图给出的信息来动态地做出决定而使搜索次数大大降低;
形式化方法不利用图给出的信息,而仅通过数学的形式分析;

算法通过一个估价函数f(h)来估计图中的当前点p到终点的距离,并由此决定它的搜索方向;
当这条路径失败时,它会尝试其他路径;
对于A*,估价函数=当前值+当前位置到终点的距离,即f(p)=g(p)+h(p),每次扩展估价函数值最小的一个;

对于K短路算法来说,g(p)为当前从s到p所走的路径的长度;h(p)为点p到t的最短路的长度;
f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;

为了加速计算,h(p)需要在A*搜索之前进行预处理, 因为本题是有向图,所以将原图的所有边反向,再从终点t做一次单源点最短路径就能得到每个点的h(p)了;

算法步骤:
(1),将有向图的所有边反向,以原终点t为源点,求解t到所有点的最短距离;
(2),新建一个优先队列,将源点s加入到队列中;
(3),从优先级队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数;
如果当前为t的第k次出队,则当前路径的长度就是s到t的第k短路的长度,算法结束;
否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先级队列;

---------------------------------

以上这段话摘自其它博客,感觉写的不错...

代码:

 #include<iostream>
#include<queue>
#include <string.h>
#include<algorithm>
#define MAXN 1010
using namespace std; const int inf=0x3f3f3f3f;
int dist[MAXN]; //**dist[i]记录此时源点到i的最短距离
bool vis[MAXN]; //***标记点是否在队列中
int cnt[MAXN]; //***cnt[i]记录i节点入队次数,判断是否存在负权环
vector<pair<int, int> >mp[MAXN*], mp2[MAXN*];//mp储存原图,mp2存储反向图
struct node{
int point;
int g, f;
friend bool operator<(node a, node b){
return a.f==b.f?a.g>b.g:a.f>b.f;
}
}; int A_star(int s, int e, int n, int k){
priority_queue<node> q;
int cnt=;//***当前为第cnt短路
if(s==e){//**本题题要求一定要经过其他点,即若终点和起点相同的话即为求第k+1短路
k++;
}
if(dist[s]==inf){//***终点点不可达
return -;
}
node node1;
node1.point=s;
node1.g=;
node1.f=dist[s]+node1.g;
q.push(node1); while(!q.empty()){
node cc=q.top(); //***将估价值最小的节点出队
q.pop();
if(cc.point==e){//**求出第cnt短路
cnt++;
}
if(cnt==k){//***已求出第k短路
return cc.g;
}
for(int i=; i<mp[cc.point].size(); i++){//**遍历当前节点的所有邻节点
node node2;
node2.point=mp[cc.point][i].first;
node2.g=cc.g+mp[cc.point][i].second;
node2.f=node2.g+dist[node2.point];
q.push(node2);
}
}
return -;
} bool spfa(int n, int s){ //**注意这里的n的实参是e,求出反向图中所有节点到n的最短距离,即所有节点到e的最短距离,即求出 h(x)
memset(dist, 0x3f, sizeof(dist));
queue<int> q;
while(!q.empty()){
q.pop();
}
q.push(s);
dist[s]=;
cnt[s]+=;
vis[s]=true;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=; i<mp2[u].size(); i++){
int point=mp2[u][i].first;
if(dist[point]>dist[u]+mp2[u][i].second){ //**松驰操作
dist[point]=dist[u]+mp2[u][i].second;
if(!vis[point]){ //***若此点不在队列中则将其入队
vis[point]=true;
q.push(point);
cnt[point]++;
if(cnt[point]>n){ //***判断是否存在负权环
return false;
}
}
}
}
}
return true;
} int main(void){
ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n, m;
int x, y, z;
cin >> n >> m;
while(m--){
cin >> x >> y >> z;
mp[x].push_back(make_pair(y, z));
mp2[y].push_back(make_pair(x, z)); //**建立反向图
}
int s, e, k;
cin >> s >> e >> k;
spfa(n, e);
int res=A_star(s, e, n, k);
cout << res << endl;
return ;
}

poj2449(k短路&A_star模板)的更多相关文章

  1. POJ2449 K短路模板

    #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...

  2. POJ2449:K短路

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

  3. A_star poj2449 k短路

    赛后填坑系列QAQ 贴代码呀 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  4. ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)

    Made In Heaven One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. ...

  5. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  6. upc组队赛15 Made In Heaven【第K短路 A*】

    Made In Heaven 题目描述 One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with ...

  7. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  8. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  9. A* K短路

    注:\(A*\) 求解K短路效率极其低下,时间复杂度\(O(nklog\ n)\),空间视题目而定,因为本质是爆搜,可求解数据范围较小的题目. 我们使用\(A*\)求解k短路: 首先需要预处理出估价函 ...

随机推荐

  1. sql语法值ORACLE简单介绍

    版权声明:本文为[博主](https://zhangkn.github.io)原创文章,未经博主同意不得转载.https://creativecommons.org/licenses/by-nc-sa ...

  2. VIM中使用tab键自动完成(vim tab键自动补全 )插件supertab

    supertab.vmb 这个插件好好用, Tab自动补全 http://www.vim.org/scripts/script.php?script_id=1643 安装步骤: 1.下载 supert ...

  3. gradlew tasks

    D:\AndroidWorkSpace\Qi\LocalM>gradlew tasks > Configure project : AAAA > Configure project ...

  4. Antler 工具使用(.g 转.java / .cs)

    1. JAVA环境 2. Antler 工具包: antlr-3.5.1-complete-no-st3.jar 路径加入classpath 3. cmd命令行: java org.antlr.Too ...

  5. 一小时搞明白自定义注解(Annotation)

    原文链接:http://blog.csdn.net/u013045971/article/details/53433874 什么是注解 Annotation(注解)就是Java提供了一种元程序中的元素 ...

  6. less的安装使用和入门实践

    1.简介 LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便CSS的编写和维护. LESSCSS可 ...

  7. Mybatis中的like模糊查询

    1.  参数中直接加入%% param.setUsername("%CD%");      param.setPassword("%11%"); <sel ...

  8. Memcached HA架构探索

    https://code.google.com/p/memagent/ 标签:memcached magent 高可用 HA 架构原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...

  9. HihoCoder1649 : 漏写的数字([Offer收割]编程练习赛38)(模拟题)

    描述 小A今年刚上幼儿园,正在学习写100以内的数字.幼儿园的老师留了一项作业,要求小A从某个100以内的数X开始一直写到另一个100以内的数Y(Y - X > 1). 不过粗心的小A在作业中漏 ...

  10. Scrapy,终端startproject,显示错误TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

    F:\python_project\test>scrapy startproject spz Traceback (most recent call last): File "d:\p ...