转载请注明出处,谢谢: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. 转载:js 创建对象、属性、方法

    1,自定义对象. 根据JS的对象扩展机制,用户可以自定义JS对象,这与Java语言有类似的地方. 与自定义对象相对应的是JS标准对象,例如Date.Array.Math等等. 2,原型(prototy ...

  2. glib源码安装使用方法

    glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事 ...

  3. IOS 特定于设备的开发:监测Retina支持

    近年来,Apple在其旗舰设备上引入了Retina显示屏.根据Apple的说法,他的像素密度非常高,足以使人眼无法区分单独的像素. UIScreen类提供了一种容易的方式,用于监查当前设备是否提供了内 ...

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">的含义

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/x ...

  5. JAVA并发,CountDownLatch使用

    该文章转自:http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html CountDownLatch 1.类介绍 一 ...

  6. strdup函数的使用方法

    函数名: strdup 功  能: 将串复制到新建的位置处 用  法: char *strdup(char *str): 这个函数在linux的man手冊里解释为: The strdup() func ...

  7. g711u与g729比較编码格式

    •711a-编解码格式为G.711 alaw •g711u-编解码格式为G.711 ulaw (the default) •g729-编解码格式为G.729 •g729a-编解码格式为G.729a 上 ...

  8. 2014年辛星Javascript解读第二节

    本小节我们解说一下Javascript的语法,尽管js语言很easy,它的语法也相对好学一些,可是不学总之还是不会的,因此,我们来一探到底把. ********凝视************* 1.我们 ...

  9. c++ 覆盖、重载、隐藏

    函数重载: 1.相同的范围内(即同一类中) 2.函数名相同: 3.参数不同: 4.virtual关键字可有可无: 函数覆盖:虚函数的功能.动态多态 (父类中必须有virtual)========派生类 ...

  10. 与html相关的知识点整理

    梳理html时发现的一些问题.总结一下,答案大都从网上找来. 一.html 与 htm 的区别 没有本质的区别..htm是在win32时代,系统只能识别3位扩展名时使用的.现在一般都使用.html. ...