poj2449 Remmarguts' Date【A*算法】
转载请注明出处,谢谢: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*算法】的更多相关文章
- POJ2449 Remmarguts' Date A*算法
题意是让求从st的ed第k短路... 考虑A*算法:先把终点到每个点最短路跑出来(注意要建反图),当做估价函数h(u),然后跑A* 每次取出总代价最小的,即g(u)+h(u)最小的进行扩展,注意如果u ...
- [poj2449]Remmarguts' Date(spfa+A*)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Remmarguts' Date Time Limit: 4000MS Mem ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- POJ2449 Remmarguts' Date
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- POJ2449 Remmarguts' Date 第K短路
POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...
- poj2449 Remmarguts' Date K短路 A*
K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
随机推荐
- WebApi服务
WCF 它利用TCP.HTTP.MSMQ等传输协议构建“契约先行”的服务.WCF最初为基于SOAP的服务而设计[xml],繁琐.冗余.慢.沉重 WebApi 基于http协议,轻量级的,支持URL路由 ...
- SnappyDB—Android上的NoSQL数据库简介
参考:http://www.open-open.com/lib/view/open1420816891937.html 参考:http://android-arsenal.com/details/1/ ...
- IQC,QA,FQC,OQC,IPQC的定义与职责
进货检验员(IQC):CLInetLabIQC(以下简称IQC)是CLInet在多年开发维护EQA(实验室间质量评价)系统后,成功开发的一套完善的实验室内部的质量评价.质量控制的软件.它不仅包含了每家 ...
- QT 一些非常常用的操作
一 如果在窗体关闭前自行判断是否可关闭二 如何用打开和保存文件对话框 三 如何使用警告.信息等对话框 四 在Windows下Qt里为什么没有终端输出五 想在源代码中直接使用中文 ...
- HDU 3362 Fix
题目大意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的,当一个没固定的点和两个固定了的点连接后,该点就被间接固 ...
- java对象的比较分析
关于对象的比较我们可以通过以下三种手段来实现 一.利用"=="比较引用 Java中,当比较简单类型变量时用"==",只要两个简单类型值相等即返回ture,否则返 ...
- data pump(数据泵)
先给出oracle给出的一个定义: “Oracle Data Pump technology enables very high-speed movement of data and metadata ...
- <td style="word-break:break-all"> 在html中控制自动换行
在html中控制自动换行 其实只要在表格控制中添加一句 <td style="word-break:break-all">就搞定了. 其中可能对英文换行可能会分开一 ...
- The Building Blocks-Components of EA part 1- Information and Strategy
1. Zachman Framework Presented as matrix of Rows and Columns representing domain of interest and lev ...
- 如何在windows server 2012 R2 部署WEB项目
tip: 今天发布项目到windows server 2012 R2上面. 没有接触过,其实很简单,看图: 这是安装IIS成功后显示的总图: 二.点击Manage ,选择Add Roles and F ...