Made In Heaven(A*算法初步学习)

2023-11-08 06:21
文章标签 算法 学习 初步 made heaven

本文主要是介绍Made In Heaven(A*算法初步学习),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接

One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci the father somehow knows it and wants to stop her. There are NNN spots in the jail and MMM roads connecting some of the spots. JOJO finds that Pucci knows the route of the former (K−1)(K-1)(K−1)-th shortest path. If Pucci spots JOJO in one of these K−1K-1K−1 routes, Pucci will use his stand Whitesnake and put the disk into JOJO's body, which means JOJO won't be able to make it to the destination. So, JOJO needs to take the KKK-th quickest path to get to the destination. What's more, JOJO only has TTT units of time, so she needs to hurry.

JOJO starts from spot SSS, and the destination is numbered EEE. It is possible that JOJO's path contains any spot more than one time. Please tell JOJO whether she can make arrive at the destination using no more than TTT units of time.

Input

There are at most 505050 test cases.

The first line contains two integers NNN and MMM (1≤N≤1000,0≤M≤10000)(1 \leq N \leq 1000, 0 \leq M \leq 10000)(1≤N≤1000,0≤M≤10000). Stations are numbered from 111 to NNN.

The second line contains four numbers S,E,KS, E, KS,E,K and TTT ( 1≤S,E≤N1 \leq S,E \leq N1≤S,E≤N, S≠ES \neq ES≠E, 1≤K≤100001 \leq K \leq 100001≤K≤10000, 1≤T≤1000000001 \leq T \leq 1000000001≤T≤100000000 ).

Then MMM lines follows, each line containing three numbers U,VU, VU,V and WWW (1≤U,V≤N,1≤W≤1000)(1 \leq U,V \leq N, 1 \leq W \leq 1000)(1≤U,V≤N,1≤W≤1000) . It shows that there is a directed road from UUU-th spot to VVV-th spot with time WWW.

It is guaranteed that for any two spots there will be only one directed road from spot AAA to spot BBB (1≤A,B≤N,A≠B)(1 \leq A,B \leq N, A \neq B)(1≤A,B≤N,A≠B), but it is possible that both directed road <A,B><A,B><A,B> and directed road <B,A><B,A><B,A> exist.

All the test cases are generated randomly.

Output

One line containing a sentence. If it is possible for JOJO to arrive at the destination in time, output "yareyaredawa" (without quote), else output "Whitesnake!" (without quote).

样例输入复制

2 2
1 2 2 14
1 2 5
2 1 4

样例输出复制

yareyaredawa

先说说A*算法;

我也是初学者,对A*算法懂得不多,看了许多别人的博客,他们写的都很透彻,同时也很专业,

我就用不专业的话说说我对A*算法的理解;

大佬请略过。。。。。。。。。。。。

A*算法重要的应该是它能起到一个预知的效果,看别人的博客都知道一个函数F=G+H;而他的最重要的在于H函数;

H函数的作用是用来判断这个点到终点的距离,就像图中的H和G,g记录了你走过的距离,h记录着当前的到终点你还要走的距离,一半H需要自己通过计算得到,如本题的H就是可以由终点计算到其他点的距离得到每一个点到终点的距离,所以H函数写的好的程序就好,而对于当前的该怎样走才是最短的,就需要比较它周围的点的G加上H的值,值越小,到终点的距离越大。有时候H很难算出这时就可以用一些特殊的方式求H值比如直接取两点距离,但是H需要<=实际的值,如果估计的比实际的大,那估计也很难的出正确结果,(反正我是不知道,我是一个很菜的人);求H的距离很重要,这或许就是别人博客中说H重要,关系到算法好坏的一个点了。。。。。。

有了H,G就只需要记录就行了,走一步记一步,f主要是运用在选择上。

我的A*算法只能给刚开始学的人一个小小的理解,同时也是让我自己能更加理解这道题才写的这篇博客。。。。。

代码

#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;
#define N 10005
typedef pair<int,int> P;
vector<P>vs[N],rvs[N];
int s,e,k,t;
int dist[N];//记录H
struct Node
{int v;//当前点int g;int f;bool operator <(const Node &a)const{return f>a.f;//用来比较F大小}
};
void dij(int e)//这个函数用来求H
{priority_queue<P, vector<P>, greater<P> >que;que.push(P(0,e));memset(dist,0x3f3f3f3f,sizeof(dist));dist[e]=0;while(que.size()){P t=que.top();que.pop();for(int it=0; it<(int)rvs[t.second].size(); it++){int v=rvs[t.second][it].first;int w=rvs[t.second][it].second;if(dist[v]>w+dist[t.second]){dist[v]=w+dist[t.second];que.push(P(dist[v],v));}}}
}int sove(int s)//这个函数用来判断怎么走
{priority_queue<Node>que;//用队列que.push((Node){s,0,0});int ans=0;while(que.size()){Node t=que.top();que.pop();if(t.v==e)ans++;//到终点的次数if(ans==k)return t.g;for(int i=0; i<(int)vs[t.v].size(); i++){int v=vs[t.v][i].first;int w=vs[t.v][i].second;Node p;p.v=v;p.g=t.g+w;//G的值p.f=p.g+dist[v];//F的值que.push(p);}}return 0;
}
int main()
{int n,m;while(scanf("%d %d",&n,&m)!=EOF){scanf("%d %d %d %d",&s,&e,&k,&t);for(int i=1; i<=n; i++)vs[i].clear(),rvs[i].clear();for(int i=1; i<=m; i++){int a,b,v;scanf("%d %d %d",&a,&b,&v);vs[a].push_back(P(b,v));rvs[b].push_back(P(a,v));}dij(e);if(dist[s]==0x3f3f3f3f)//如果没有通路{cout<<"Whitesnake!\n";continue;}int sum=sove(s);if(sum>t)//时间是否在t内{cout<<"Whitesnake!\n";}else{cout<<"yareyaredawa\n";}}return 0;
}

 

这篇关于Made In Heaven(A*算法初步学习)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/368329

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操