1. 程式人生 > >ACM-ICPC 2018 瀋陽賽區網路預賽 Made In Heaven(第k短路)

ACM-ICPC 2018 瀋陽賽區網路預賽 Made In Heaven(第k短路)

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 NN spots in the jail and MM 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 KK-th quickest path to get to the destination. What's more, JOJO only has TT units of time, so she needs to hurry.

JOJO starts from spot SS, 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.

Input

There are at most 5050 test cases.

The first line contains two integers N and MM ((1≤N≤1000,0≤M≤10000). Stations are numbered from 11 to NN.

The second line contains four numbers S, E, KS,E,K and TT ( 1≤S,E≤N, S≠E, 1≤K≤10000, 1≤T≤100000000 ).

Then MM lines follows, each line containing three numbers U, VU,V and WW (1≤U,V≤N,1≤W≤1000) . It shows that there is a directed road from UU-th spot to VV-th spot with time WW.

It is guaranteed that for any two spots there will be only one directed road from spot AA to spot BB (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.

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*演算法。

#include<bits/stdc++.h>
using namespace std;

#define ee exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=1e6+5;
int n,m,s,t,k,r,cnt,p1,p2,head1[maxn],head2[maxn],vis[maxn];
ll d[maxn];
struct node{
	int to,w,next;
}e1[maxn],e2[maxn];
struct qnode{
	int v;ll w;
	qnode(int v,ll w):v(v),w(w) {}
	friend bool operator < (qnode x,qnode y)
	{
		return x.w+d[x.v]>y.w+d[y.v];
	}
};
void add1(int u,int v,int w)
{
	e1[++p1].to=v;e1[p1].w=w;e1[p1].next=head1[u];head1[u]=p1;
}
void add2(int u,int v,int w)
{
	e2[++p2].to=v;e2[p2].w=w;e2[p2].next=head2[u];head2[u]=p2;
}
void spfa()//反向圖,t到其他點的距離 
{
	queue<int> q;
	q.push(t);
	for(int i=1; i<=n; i++)d[i]=inf;
	mem(vis,0);
	d[t]=0;
	vis[t]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		vis[u]=0;
		for(int i=head1[u]; i; i=e1[i].next)
		{
			int v=e1[i].to;
			int w=e1[i].w;
			if(d[v]>d[u]+w)
			{
				d[v]=d[u]+w;
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
}

ll astar()//A*演算法 
{
	if(d[s]==inf)return -1;
	
	priority_queue<qnode> pe;
	pe.push(qnode(s,0));
	cnt=0;
	while(!pe.empty())
	{
		qnode u=pe.top();pe.pop();
		
		if(u.v==t)
		{
			cnt++;
			if(cnt==k)return u.w;
		}
		for(int i=head2[u.v]; i; i=e2[i].next)
		{
			int v=e2[i].to;
			pe.push(qnode(v,u.w+e2[i].w));
		}
	}
	return -1;
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		mem(head1,0);p1=0;
		mem(head2,0);p2=0;
		scanf("%d%d%d%d",&s,&t,&k,&r);
		while(m--)
		{
			int u,v,w;scanf("%d%d%d",&u,&v,&w);
			add1(v,u,w);//反向圖 
			add2(u,v,w);
		}
		spfa();
		ll ans=astar();
		if(ans==-1||ans>r)puts("Whitesnake!");
		else puts("yareyaredawa");
	}
	return 0;
}