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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值