http://poj.org/problem?id=2449

A*搜索求K短路。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
const int N = 1001;
#define inf (1<<29)
struct nod{int x,val;nod() {}friend bool operator < (const nod& a,const nod& b){return b.val<a.val;}
};
vector<struct nod> g[N],r[N];
priority_queue<nod,vector<nod> > Q;
int n,m,S,T,K,dist[N],out[N],size;
void dijkstra(){bool vi[N];vector<struct nod>::iterator iter;for(int i=1;i<=n;i++){vi[i]=0;dist[i]=inf;}dist[T]=0;while(1){int k=-1;for(int i=1;i<=n;i++)if(!vi[i] && (k==-1 || dist[i]<dist[k])) k=i;if(k==-1) break;vi[k]=1;vector<struct nod>::iterator iter;for(iter=r[k].begin();iter!=r[k].end();iter++)if(!vi[(*iter).x] && dist[(*iter).x]>dist[k]+(*iter).val)dist[(*iter).x]=dist[k]+(*iter).val;}
}
int astar(){dijkstra();nod v;v.x=S;v.val=dist[S];Q.push(v);vector<struct nod>::iterator iter;for(int i=0;i<=n;i++) out[i]=0;while(!Q.empty() && out[T]<K){v=Q.top();Q.pop();if(out[v.x]>=K) continue;if(v.x==T){out[v.x]++;if(v.x==T && out[v.x]==K) return v.val;}for(iter=g[v.x].begin();iter!=g[v.x].end();iter++){if(out[(*iter).x]>=K) continue;nod tmp;tmp.val=v.val-dist[v.x]+(*iter).val+dist[(*iter).x];tmp.x=(*iter).x;Q.push(tmp);}}return -1;
}
int main(){while(~scanf("%d%d",&n,&m)){int a,b,w;for(int i=1;i<=n;i++) { g[i].clear();r[i].clear(); }while(m--){scanf("%d%d%d",&a,&b,&w);nod tmp;tmp.x=b;tmp.val=w;g[a].push_back(tmp);tmp.x=a;r[b].push_back(tmp);}scanf("%d%d%d",&S,&T,&K);if(S==T) K++;int ans=astar();printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/aiiYuu/archive/2013/04/01/2993815.html

Poj2449 Remmarguts' Date 【A*搜索】K短路相关推荐

  1. POJ 2449 Remmarguts' Date(第K短路 + A* + 最短路)题解

    题意:找出第k短路,输出长度,没有输出-1 思路:这题可以用A*做.A*的原理是这样,我们用一个函数:f = g + h 来表示当前点的预期步数,f代表当前点的预期步数,g代表从起点走到当前的步数,h ...

  2. poj2449 Remmarguts' Date(第k短路问题)(A*+spfa/dijkstra)

    思路来源 https://blog.csdn.net/berrykanry/article/details/78345894(通俗易懂解释好评) https://www.cnblogs.com/yyf ...

  3. A* 第k短路详解 (详尽)

    首先分享一个我学习的博客文章: Poj2449-A*初步+k短路 看着他的题解学会了k短路..%%% 然后我就大致说一说k短路的求法吧.. 首先我们来看看A*. A*,启发式搜索,是一种较为有效的搜索 ...

  4. 第K短路+严格第K短路

    所谓K短路,就是从s到t的第K短的路,第1短就是最短路. 如何求第K短呢?有一种简单的方法是广度优先搜索,记录t出队列的次数,当t第k次出队列时,就是第k短路了.但点数过大时,入队列的节点过多,时间和 ...

  5. POJ 2449 Remmarguts' Date [第k短路]

    Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Descripti ...

  6. POJ 2449 Remmarguts' Date(k短路模板)

    link:https://vjudge.net/problem/POJ-2449 前面输入与大多最短路题相同 最后一行输入s,t,k 求从s到t的第K短路 wiki link: https://en. ...

  7. POJ - 2449 Remmarguts' Date(第k短路:spfa+A*)

    题目链接:点击查看 题目大意:给出一个有向图,求第k短路 题目分析:偷学了一波A*,本来以为是多难的算法,其实就是bfs+优先队列的升级版,之前看的那些博客写的都太深奥了,以至于看了一半啥都没看懂然后 ...

  8. Remmarguts' Date(POJ2449+最短路+A*算法)

    题目链接:http://poj.org/problem?id=2449 题目: 题意:求有向图两点间的k短路. 思路:最短路+A*算法 代码实现如下: 1 #include <set> 2 ...

  9. poj2449(k短路算法)

    K 短路问题(A* 启发式广搜) 1.k 短路问题就是最短路问题的延申,要找出第 k 短的路径.用广搜进行路径查找,第一次找到的 就是最短路,第二次找到的是第 2 短 路⋯以此类推.所以我们只需要一直 ...

最新文章

  1. Xamarin的环境搭建Xamarin.iOS
  2. 韩顺平java笔记 第1讲 内容介绍 项目演示 原理剖析
  3. Adnroid体系与系统架构
  4. matlab simulink_简单五步实现 MATLAB/Simulink 锂电池建模
  5. MongoDB | Mysql亿级别---数据生成及高效率导入
  6. 如何在NVIDIA(英伟达)官网下载老版本Toolkit-SDK---例如下载CUDA Toolkit 8.0
  7. scikit-learn的基本使用
  8. c/c++再学习:C与Python相互调用
  9. Ajax 超完整教程
  10. 使用爬虫自动登录QQ空间
  11. 【校招VIP】产品行测考察之逻辑推理
  12. Linux---ALSA音频工具arecord、aplay、amixer使用
  13. asp.net WebResource.axd请求报404错误
  14. DBeaver出现:The Network Adapter could not establish the connection 已解决
  15. faker 无敌了,专注于制作假数据
  16. linux运维工程师 pdf下载,linux运维工程师命令.pdf
  17. php商城积分兑换商品功能,ECSHOP积分商城添加金额+积分兑换功能
  18. android红外遥控驱动
  19. 迅雷怎样打开html文件类型,迅雷看看怎么打开网页链接?迅雷看看打开种子文件方法...
  20. Windows模拟器推荐

热门文章

  1. hadoop+hive+flink+hbase交互的版本兼容性
  2. The configured Task Off-Heap Memory 0 bytes is less than the least required Python worker Memory 79
  3. It's highly recommended that you fix the library with 'execstack -c libfile', or link it with '-z
  4. Name node is in safe mode解决
  5. xfce右键open in terminal失效问题解决
  6. 消除ubuntu16.04自带的alt快捷键
  7. 数值方法:插值与多项式逼近
  8. 人脸变形算法——MLS
  9. android word转html标签,如何将Word转换为网页html格式的方法(附代码清理方法)
  10. php7和php8内核有区别吗,不要在PHP7中踩这些坑