Made In Heaven(ACM-ICPC 2018 沈阳赛区网络预赛)

版权声明:本文为博主原创文章,转载请附上注明就行_(:з」∠)_。 https://blog.csdn.net/vocaloid01/article/details/82532314
  • 1000ms
  • 131072K

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.

Input

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,K,and 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.

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

样例输出

扫描二维码关注公众号,回复: 3189658 查看本文章
yareyaredawa

题目来源

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

题解:

求第K短路,是A*算法的典型题而且还是个模板题。拿板子改改注意不存在第K短路时输出“Whitesnake!”就AC了。

同类型题:POJ——2449 Remmarguts' Date

代码:

#include <cstring>
#include <cstdio>
#include <queue>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
 
int N,M,S,K,End,T;
 
struct Edge{
	int to,w,next;
}E[100*MAXN],RE[100*MAXN];//正向边,Astar用。反向边,Spfa用。 
 
int head[MAXN],rhead[MAXN];
int tot;
 
struct A{
	int f,g,id;//id是点的号。 
	//Astar核心式子:f=g+h,这里h(i)=dis[i]。 
	bool operator < (const A &b)const {
		if(f == b.f)return  g > b.g;
		return f > b.f;
	}
};
 
inline void Add(int from,int to,int w){
	E[++tot].to = to;
	E[tot].w = w;
	E[tot].next = head[from];
	head[from] = tot;
	RE[tot].to = from;
	RE[tot].w = w;
	RE[tot].next = rhead[to];
	rhead[to] = tot;
}
 
int dis[MAXN];
bool used[MAXN];
 
/*求出终点到各点的最短长度dis[i]
然后用这个长度来作为后面A*算法的启发式信息h(i)。*/
void Spfa(int from){
	memset(dis,INF,sizeof dis);
	//memset(used,false,sizeof used);
	dis[from] = 0;
	queue<int> Q;
	Q.push(from);
	used[from] = true;
	while(!Q.empty()){
		int t = Q.front();
		Q.pop();
		used[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(!used[RE[i].to]){
					used[RE[i].to] = true;
					Q.push(RE[i].to);
				}
			}
		}
	}
}
 
int Astar(int from,int to){
	int cnt = 0;
	priority_queue<A> Q;
	if(from == to)++K;//坑点,注意判断题意中相同点算不算最短路 
	if(dis[from] == INF)return -1;//起点与终点不连通。
	A t,tt;
	t.id = from,t.g = 0,t.f = t.g + dis[from];
	Q.push(t);
	while(!Q.empty()){
		tt = Q.top();
		Q.pop();
		if(tt.id == to){
			++cnt;
			if(cnt == K)return tt.g;//第K次到达时就是结果。 
		}
		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;//没有第K短路 
}
 
inline void init(){
	tot = 0;
	memset(head,0,sizeof head);
	memset(rhead,0,sizeof rhead);
}
 
int main(){
	
	while(scanf("%d %d",&N,&M) == 2){
		init();
		scanf("%d %d %d %d",&S,&End,&K,&T);
		int x,y,w;
		for(int i=1 ; i<=M ; ++i){
			scanf("%d %d %d",&x,&y,&w);
			Add(x,y,w);
		}
		Spfa(End);
		int re = Astar(S,End);
		if(re <= T && re != -1)printf("yareyaredawa\n");
		else printf("Whitesnake!\n");
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/vocaloid01/article/details/82532314