题目描述
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 N spots in the jail and M roads connecting some of the spots. JOJO finds that Pucci knows the route of the former (K-1)-th shortest path. If Pucci spots JOJO in one of these K-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 K-th quickest path to get to the destination. What’s more, JOJO only has T units of time, so she needs to hurry.
JOJO starts from spot S, and the destination is numbered E. 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 T units of time.
输入
There are at most 50 test cases.
The first line contains two integers N and M (1≤N≤1000,0≤M≤10000). Stations are numbered from 1 to N.
The second line contains four numbers S, E, Kand T( 1≤S,E≤N,S≠E,1≤K≤10000, 1≤T≤100000000).
Then M lines follows, each line containing three numbers U, V and W (1≤U,V≤N,1≤W≤1000) . It shows that there is a directed road from U-th spot to V-th spot with time W.
It is guaranteed that for any two spots there will be only one directed road from spot A to spot B(1≤A,B≤N,A≠B), but it is possible that both directed road <A,B> and directed road <B,A> exist.
All the test cases are generated randomly.
输出
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*算法的模板题,注意其中的一些不同
模板引用自 https://blog.csdn.net/vocaloid01/article/details/82532314
代码实现
#include <bits/stdc++.h>
using namespace std;
const int N=1005;
const int INF=0x3f3f3f3f;
int n,m,s,k,en,t;
struct edge
{
int to,w,next;
}E[100*N],RE[100*N];
int head[N],rhead[N];
int cnt;
struct A
{
int f,g,id;
bool operator < (const A &b)const
{
if(f==b.f) return g>b.g;
return f>b.f;
}
};
void add(int u,int v,int w)
{
E[++cnt].to=v;
E[cnt].w=w;
E[cnt].next=head[u];
head[u]=cnt;
RE[cnt].to=u;
RE[cnt].w=w;
RE[cnt].next=rhead[v];
rhead[v]=cnt;
}
int dis[N];
bool vis[N];
void spfa(int from)
{
memset(dis,INF,sizeof(dis));
dis[from]=0;
queue<int> q;
q.push(from);
vis[from]=true;
while(!q.empty())
{
int t=q.front();
q.pop();
vis[t]=false;
for(int i=rhead[t];i;i=RE[i].next)
{
if(dis[RE[i].to]>dis[t]+RE[i].w)
{
dis[RE[i].to]=dis[t]+RE[i].w;
if(!vis[RE[i].to])
{
vis[RE[i].to]=true;
q.push(RE[i].to);
}
}
}
}
}
int Astar(int u,int v)
{
int tot=0;
priority_queue<A>q;
if(u==v) k++;
if(dis[u]==INF) return -1;
A t,tt;
t.id=u;
t.g=0;
t.f=t.g+dis[u];
q.push(t);
while(!q.empty())
{
tt=q.top();
q.pop();
if(tt.id==v)
{
tot++;
if(tot==k) return tt.g;
}
for(int i=head[tt.id];i;i=E[i].next)
{
t.id=E[i].to;
t.g=tt.g+E[i].w;
t.f=t.g+dis[t.id];
q.push(t);
}
}
return -1;
}
inline void init()
{
cnt=0;
memset(head,0,sizeof(head));
memset(rhead,0,sizeof(rhead));
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
scanf("%d%d%d%d",&s,&en,&k,&t);
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
spfa(en);
int re=Astar(s,en);
if(re<=t && re!=-1) printf("yareyaredawa\n");
else printf("Whitesnake!\n");
}
return 0;
}