转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303855.html   ---by 墨染之樱花

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

【题目描述】:给出图,求从起点到终点的第K短路

【思路】:求第K短的算法基于BFS搜索,当终点出队K次时,所走的总距离就是第K短路,不过这样那些不该走的路会被反复的走,造成许多空间时间浪费,这时候就要用到启发式的A*搜索。关于此算法的详细内容请自行查阅资料,这里简单的提一下。A*算法利用一个估价函数f(n)=g(n)+h(n),其中g(n)表示起点s到点n所耗费的实际代价,h(n)表示n到终点t所估计的代价,也就是说理想情况下从n到t还要耗费的代价,h(n)越接近真实值算法速度越快(实际上BFS就是h(n)始终为0的A*特例)。在网格图中h(n)可以为欧几里得距离或者是曼哈顿距离,在此题中,我们将从n到t的最短路作为h(n)。完成估价函数以后,我们以估价函数为优先级进行搜索(可以用优先队列实现,f(n)小的先搜索),这样就能大致保证搜索路径始终朝着我们想要的方向走,从而加快搜索速度。

 #include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 1000+10
#define eps 1e-10
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second int V,E,S,T,K;
int d[MAXN];
VII G[MAXN];
VII rG[MAXN]; //对反图dijkstra,求出每个点到终点的最短路,作为预计代价h(n)
int cnt[MAXN]={}; //记录出队次数 void dijkstra(int s)
{
priority_queue<PII,VII,greater<PII> > Q;
fill(d,d+V,INF);
d[s]=;
Q.push(MP(d[s],s));
while(!Q.empty())
{
PII p=Q.top();Q.pop();
int v=p.Y;
if(d[v]<p.X)
continue;
REP(i,rG[v].size())
{
PII e=rG[v][i];
if(d[e.X]>d[v]+e.Y)
{
d[e.X]=d[v]+e.Y;
Q.push(MP(d[e.X],e.X));
}
}
}
} struct node //A*搜索用节点
{
int num,g,h;
bool operator<(const node &b)const
{
return g+h!=b.g+b.h?g+h>b.g+b.h:g>b.g; //以f=g+h为第一关键字,g为第二关键字
}
node(int _num=,int _g=,int _h=){num=_num;g=_g;h=_h;}
}; int astar(int s,int t)
{
priority_queue<node> Q;
node st(s,,d[s]);
Q.push(st);
while(!Q.empty())
{
node temp=Q.top();Q.pop();
int u=temp.num,ug=temp.g,uh=temp.h;
cnt[u]++;
if(u==t && cnt[t]==K)
return ug;
if(cnt[u]>K)
continue;
REP(i,G[u].size())
{
int v=G[u][i].X,cost=G[u][i].Y;
node next(v,ug+cost,d[v]);
Q.push(next);
}
}
return -;
} int main()
{_
scanf("%d%d",&V,&E);
REP(i,E)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
x--;y--;
G[x].PB(MP(y,c));
rG[y].PB(MP(x,c));
}
scanf("%d%d%d",&S,&T,&K);
S--;T--;
if(S==T) //起点与终点相同时,最短路显然是0,不过不能算 ,所以k++
K++;
dijkstra(T); //反向dijkstra,求出每个点到T的最短路,作为h(i)
printf("%d\n",astar(S,T));
return ;
}

poj2449 Remmarguts' Date【A*算法】的更多相关文章

  1. POJ2449 Remmarguts' Date A*算法

    题意是让求从st的ed第k短路... 考虑A*算法:先把终点到每个点最短路跑出来(注意要建反图),当做估价函数h(u),然后跑A* 每次取出总代价最小的,即g(u)+h(u)最小的进行扩展,注意如果u ...

  2. [poj2449]Remmarguts' Date(spfa+A*)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Remmarguts' Date Time Limit: 4000MS   Mem ...

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

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

  4. POJ2449 Remmarguts' Date

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...

  5. POJ2449 Remmarguts' Date 第K短路

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

  6. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  7. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

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

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

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

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

随机推荐

  1. 定义一个runtime的Annotation

    import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(value ...

  2. break的使用for循环嵌套

    /* Name:break的使用for循环嵌套 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 02:54:04 Description:以下代码 ...

  3. 转:Grunt:任务自动管理工具

    Grunt:任务自动管理工具 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 安装 命令脚本文件Gruntfile.js Gruntfile.js实例:gru ...

  4. ViewPager不能高度自适应?height=wrap_content 无效解决办法

    ViewPager用的很多,主要用啦展示广告条.可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的.. 解决办 ...

  5. 求高手帮忙解决一下问题Java Web Cookie实例

    package cn.com; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedHashM ...

  6. iOS中Block介绍(二)内存管理与其他特性

    我们在前一章介绍了block的用法,而正确使用block必须要求正确理解block的内存管理问题.这一章,我们只陈述结果而不追寻原因,我们将在下一章深入其原因. 一.block放在哪里 我们针对不同情 ...

  7. CreateFileMapping使用方法

    CreateFileMapping的MSDN翻译和使用心得   測试创建和打开文件映射的时候老是得到"句柄无效"的错误, 细致看了MSDN以后才发觉是函数认识不透, 这里把相关的解 ...

  8. C#隐藏桌面图标和任务栏

    最近因为项目需要需要实现桌面图标和任务状态栏的隐藏功能,实现的方式很多,比如修改注册表值,调用windows API函数等.经过一番的查阅,这个功能暂时实现了,或许不是很好的方法,但是我预期的效果达到 ...

  9. 开始学习Lucene

    最近百度的魏则西事件闹的沸沸扬扬,突然有个想法:是否百度的中文搜索目前还没有人能挑战它的地位呢? 哈哈,想的太多了,正巧毕业设计就和搜索有关,当时只是大致了解了概念:如分词.排序.索引.爬虫等,并以此 ...

  10. AC自动机妙用

    理解题意之后,很自然的想到了用AC自动机搞,结果网上一搜,全是暴搜,按照自己的思想,AC自动机搞起,果然在提交了数次之后,看到了Accept. AC自动机需要三个步骤: 第一步:建立字典树: 第二步: ...