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
题目来源
题解:
k短路问题,解决方案是最短路+A*
坑:
在A*算法中无法到达的需要特判,然后输入输出要用scanf,建边的时候要注意
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define N 1100
#define inf 1000000000
int n,m,s,e,k,t;
long long ans;
int cnt;
vector<pair<int,int>>maps[N];
vector<pair<int,int>>maps2[N];
int flag[N];
int diss[N];
struct node{
int u;
int d;
node(int uu,int dd):u(uu),d(dd){}
friend bool operator<(const node &a,const node &b)
{
return diss[a.u]+a.d>diss[b.u]+b.d;
}
};
void SPFA()
{
for(int i=1;i<=n;++i)
{
diss[i]=inf;
}
memset(flag,0,sizeof(flag));
flag[e]=1;
diss[e]=0;
int n1,n2;
n1=e;
queue<int>q;
q.push(n1);
while(!q.empty())
{
n1=q.front();
q.pop();
flag[n1]=0;
for(int i=0;i<maps[n1].size();++i)
{
n2=maps[n1][i].first;
int val=maps[n1][i].second;
if(diss[n2]>diss[n1]+val)
{
diss[n2]=diss[n1]+val;
if(!flag[n2])
{
flag[n2]=1;
q.push(n2);
}
}
}
}
}
int Astar()
{
if(diss[s]==inf)
return -1;
node n1(s,0);
cnt=0;
priority_queue<node>pq;
pq.push(n1);
while(!pq.empty())
{
n1=pq.top();
pq.pop();
if(n1.u==e)
{
cnt++;
if(cnt==k)
return n1.d;
}
for(int i=0;i<maps2[n1.u].size();++i)
{
int v=maps2[n1.u][i].first;
int val=maps2[n1.u][i].second;
pq.push(node(v,n1.d+val));
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(false);
while(scanf("%d%d",&n,&m)>0)
{
scanf("%d%d%d%d",&s,&e,&k,&t);
for(int i=1;i<=n;++i)
{
maps[i].clear();
maps2[i].clear();
}
for(int i=1;i<=m;++i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
maps[v].push_back(make_pair(u,w));
maps2[u].push_back(make_pair(v,w));
}
SPFA();
ans=Astar();
if(ans>t||ans==-1)
cout<<"Whitesnake!"<<endl;
else
cout<<"yareyaredawa"<<endl;
}
return 0;
}