POJ——2449Remmarguts' Date(A*+SPFA)
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 26504 | Accepted: 7203 | 
Description
"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."
"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"
Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!
DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station
 twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 
Input
sideway from A-th station to B-th station with time T.
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14
以前对A*有所耳闻,看得懂但是不知道如何实现,然后通过这道题有了一点理解……,这题做了两遍,第一次是看着别人的代码写的,第二次隔了段时间自己按照自己的理解写了下1A,理解一个算法还是非常重要的……
A*以我的理解就是给搜索算法一个大致的导向,尽量不往不必要的方向去搜索,也许也是因此叫做启发式搜索算法,就拿普通BFS比较吧,也就是一个退化了的A*,没有任何的导向,完全按照压入队列的时间顺序进行搜索,比如一个任意一点都可以站立的地图,人在中间,而目的地在右上角,显然BFS在到达目的地之前一定会对地图中大部分包括跟目的地相反方向的那些地方都搜索过,即越走越远,但是BFS不知道这些,只知道一层一层地扩展,即使走反了,如果有用标记或者染色,会发现地图上大部分无用、完全不必要的地区都被标记或染色过……因此BFS在数据范围较大的时候是比较慢的
而A*不一样,比BFS多了一个估价(启发)函数。先引用别的博主的一段话
所谓A*就是启发式搜索..说白了就是给BFS搜索一个顺序使得搜索更加合理减少无谓的搜索..如何来确定搜索的顺序?..也就是用一个值来表示这个值为f[x]..每次搜索取f[x]最小的进行拓展...f[x]=h[x]+g[x]其中h[x]就是当前搜索时的实际代价...估价函数要小于是对当前点到目标的代价的估计..这个估计必须小于等于实际值~~否则会出错...A*的关键也就是构造g[x]..
在图的搜索里面构造g[x]用的比较多的是欧氏距离、曼哈顿距离、切比雪夫距离(这个比较高端感觉不太用的上……)上面的紫色字体非常重要,这就是为什么要用反向的SPFA来做这题的原因,因为反着从T的单源的最短路放到正向的就不一定是某一点Si的最短路了,就可以保证反着是一定小于等于实际值的。这题还有一个小坑点就是如果一开始起点就是终点,那必须要绕一圈再回来,即k短路不能一开始就为0,VIS数组也不需要,首先这是k短路,其次这是A*启发式的,不会无脑遍历……然后就差不多可以做了……
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int M=100010;
const int N=10010;
struct info
{
int to;
int pre;
int dx;
};
struct A
{
int cur;
int g;
int h;
bool operator<(const A &b)const
{
return g+h>b.g+b.h;
}
};
info E[M],rE[M];
int head[M],rhead[M],d[N],cnt,rcnt;
priority_queue<pii>Q;
priority_queue<A>Qa;
void init()
{
MM(head,-1);
MM(rhead,-1);
MM(d,INF);
while (!Q.empty())
Q.pop();
while (!Qa.empty())
Qa.pop();
cnt=rcnt=0;
}
void add(info edge[],int &c,int Head[],int s,int t,int d)
{
edge[c].to=t;
edge[c].dx=d;
edge[c].pre=Head[s];
Head[s]=c++;
}
void spfa(int s)
{
d[s]=0;
Q.push(pii(-d[s],s));
while (!Q.empty())
{
int now=Q.top().second;
Q.pop();
for (int i=rhead[now]; ~i; i=rE[i].pre)
{
int v=rE[i].to;
if(d[v]>d[now]+rE[i].dx)
{
d[v]=d[now]+rE[i].dx;
Q.push(pii(-d[v],v));
}
}
}
}
int main(void)
{
int n,m,i,j,a,b,c,s,t,k;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(rE,rcnt,rhead,b,a,c);
add(E,cnt,head,a,b,c);
}
scanf("%d%d%d",&s,&t,&k);
spfa(t);
int r=-1;
A S;
S.cur=s;
S.g=0;
S.h=d[s];
Qa.push(S);
if(s==t)
k++;
while (!Qa.empty())
{
A now=Qa.top();
Qa.pop();
if(now.cur==t)
{
if(--k==0)
{
r=now.g;
break;
}
}
for (i=head[now.cur]; ~i; i=E[i].pre)
{
A Next=now;
int v=E[i].to;
Next.cur=v;
Next.g+=E[i].dx;
Next.h=d[v];
Qa.push(Next);
}
}
printf("%d\n",r);
}
return 0;
}
POJ——2449Remmarguts' Date(A*+SPFA)的更多相关文章
- POJ:2449-Remmarguts' Date(单源第K短路)
		Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33081 Accepted: 8993 Des ... 
- POJ 2449:Remmarguts' Date(A* + SPFA)
		题目链接 题意 给出n个点m条有向边,源点s,汇点t,k.问s到t的第k短路的路径长度是多少,不存在输出-1. 思路 A*算法是启发式搜索,通过一个估价函数 f(p) = g(p) + h(p) ,其 ... 
- POJ 3279 Fliptile(翻格子)
		POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ... 
- POJ - 3308 Paratroopers(最大流)
		1.这道题学了个单词,product 还有 乘积 的意思.. 题意就是在一个 m*n的矩阵中,放入L个敌军的伞兵,而我军要在伞兵落地的瞬间将其消灭.现在我军用一种激光枪组建一个防御系统,这种枪可以安装 ... 
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
		两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ... 
- poj 1364 King(差分约束)
		题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ... 
- poj 1201 Intervals(差分约束)
		做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ... 
- POJ 3259 Wormholes (Bellman_ford算法)
		题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ... 
- POJ 1006 Biorhythms (中国剩余定理)
		在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ... 
随机推荐
- Java类的静态块の二
			在上一篇Java类的静态块の一中介绍到:类的静态块在类加载时候执行,执行早于构造函数,并且只执行一次. 但是在下面的例子中却发现JVM并不是把把所有的static模块都执行完成再执行其他(Java果然 ... 
- azure 创建redhat镜像帮助
			为 Azure 准备基于 Red Hat 的虚拟机 从 Hyper-V 管理器准备基于 Red Hat 的虚拟机 先决条件 本部分假定你已经从 Red Hat 网站获取 ISO 文件并将 RHEL 映 ... 
- nconf修改密码
			修改nconf登录界面密码 [root@Cnyunwei config]# vi .file_accounts.php <?php/*## User/Password file for simp ... 
- Spring MVC系列[2]——参数传递及重定向
			1.目录结构 2.代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ... 
- sql中的exsits和not exsits
			select * from table where exsits(sql语句) : 括号中sql语句有数据则返回这些相关id的数据集 select * from table where not ex ... 
- Nginx常用命令介绍
			#安装nginx准备工作yum install gcyum -y install pcre-develyum install -y zlib-devel #编译安装./configuremake &a ... 
- 洛谷P1036 选数
			题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12, ... 
- Nodejs + Jshint自动化静态代码检查
			1. 目的 提交代码前能够自动化静态代码检查,提高代码质量 2. 准备 1. Nodejs安装: 官方地址:http://nodejs.org/ 安装说明:根据电脑配置下载对应的版本进行 ... 
- 一个简单的例子教会您使用javap
			javap是JDK自带的工具: 这篇文章使用下面这段简单的Java代码作为例子进行讲解. class Outer { Nested nested; Nested getNested() { retur ... 
- java 核心技术卷一笔记 6 .2.3 接口 lambda 表达式 内部类
			6.2.3 对象克隆 Cloneable 接口,这个接口指示一个类提供了一个安全的clone方法.(稍作了解) 为一个对象引用的变量建立副本时,原变量和副本都是同一个对象的引用,任何一个变量改变都 ... 
