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

题目来源

ACM-ICPC 2018 沈阳赛区网络预赛

解析:模板题,求第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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值