转载请注明出处,谢谢: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. C# 后台调用前台JS

    1.需要添加微软的类库  Interop.MSScriptControl.dll 2. var path = Path.GetFullPath("../../javascript/youzi ...

  2. 这两天写的mybatis配置文件,主要是有输出和输入的存储过程

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  3. JavaScript最全的10种跨域共享的方法

    在客户端编程语言中,如javascript和ActionScript,同源策略是一个很重要的安全理念,它在保证数据的安全性方面有着重要的意义.同源策略规定跨域之间的脚本是隔离的,一个域的脚本不能访问和 ...

  4. Vue.js——webpack

    Vue.js——60分钟webpack项目模板快速入门 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开 ...

  5. 贴片陶瓷电容的NPO、C0G、X7R、X5R、Y5V、Z5U辨析

    NPO与X7R.X5R.Y5V.Z5U神马的有啥区别?主要是介质材料不同.不同介质种类由于它的主要极化类型不一样,其对电场变化的响应速度和极化率亦不一样. 在相同的体积下的容量就不同,随之带来的电容器 ...

  6. CAD教程/视频教程/软件类专题资料免费下载整理合集

    CAD教程&视频教程类专题资料免费下载 资源列表:http://www.xiaodianlv.com/group/cad/ [1] <AUTOCAD2012中文版全套视频教程大合集> ...

  7. BlueTooth的EDR是什么

    EDR 即Enhanced data rate,是蓝牙技术中增强速率的缩写,其特色是大大提高了蓝牙技术的数据传输速率,达到了2.1Mbps ,是目前蓝牙技术的三倍.因此除了可获得更稳定的音频流传送的更 ...

  8. Vim 实用技术,第 2 部分: 常用插件(转)

    http://blog.jobbole.com/20619/ 2.1. gzip(压缩文件支持) 作者:Bram Moolenar 网站脚本编号:无(包含在 Vim 的标准发布之中) 安装说明:无 功 ...

  9. kohana(3.2)和gleez(1.1.5)的安装

    *保证在kohanna的环境下安装gleez 一.配置虚拟主机(即添加端口:例如localhost:801) 以http://www.gleezcms.com为例 1: cd /etc/apache2 ...

  10. SQL函数经常用到的mark一下

    在项目开发过程中存储过程会用到很多SQL函数,经常用到的mark一下 1.经常用到的mark 一下 经常需要把id字符以','分隔传入存储过程然后SQL语句用in去搜索但是经常是这样的情况id 经常是 ...