计蒜客 Made In Heaven (2018-ICPC-沈阳-网络赛)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)(K−1)-th shortest path. If Pucci spots JOJO in one of these K-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 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 EE. 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 TT units of time.

Input

There are at most 5050 test cases.

The first line contains two integers NN and MM (1 \leq N \leq 1000, 0 \leq M \leq 10000)(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 \leq S,E \leq N1≤S,E≤N, S \neq ES≠E, 1 \leq K \leq 100001≤K≤10000, 1 \leq T \leq 1000000001≤T≤100000000 ).

Then MM lines follows, each line containing three numbers U, VU,V and WW (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 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 \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> and directed road <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

题目来源

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


题意,给出一张有向图,有边权,找出判断是否有 第 k 短的路径花费在 t 以下

做法: A* 经典

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))

const int maxn = 1005;
const int maxm = 10005;
const int inf = 0x3f3f3f3f;

struct node {
	int v,nxt,w;
	node() {}
	node(int _v, int _nxt, int _w) {
		v = _v;
		nxt = _nxt;
		w = _w;
	}
} e[maxm],re[maxm];

struct A {
	int f,g,id;
	A() {}
	A(int _f, int _g, int _id) {
		f = _f;
		g = _g;
		id = _id;
	}
	// A*  f = g + h   h->(估计花费值) g->(真实花费权值) 
	bool operator < (const A &b)const {
		if(f == b.f) {	// 总值相同情况下,估计值小的优先 (更近)
			return g > b.g;
		}
		return f > b.f;		//总值小的优先 
	}
};

int n,m,st,ed,k,t;

int head[maxn],rhead[maxn],dis[maxn];
bool vis[maxn];
int tot;

void init() {
	mem(head,-1);
	mem(rhead,-1);
	mem(dis,inf);
	mem(vis,false);
	tot = 0;
}

void add(int u, int v, int w) {
	e[++tot] = node(v,head[u],w);
	head[u] = tot;

	re[tot] = node(u,rhead[v],w);;
	rhead[v] = tot;
}

void spfa(int start) {	// spfa 反向图求出所有点到终点最短距离
	dis[start] = 0;
	queue<int>que;
	que.push(start);
	vis[start] = true;
	while(!que.empty()) {
		int u = que.front();
		que.pop();
		vis[u] = false;
		for(int i = rhead[u]; i != -1; i = re[i].nxt) {
			int v = re[i].v;
			int w = re[i].w;
			if(dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				if(!vis[v]) {
					vis[v] = true;
					que.push(v);
				}
			}
		}
	}
}

int A_Star(int start, int end) {
	int cnt = 0;
	priority_queue<A>que;
	if(start == end) {
		k++;
	}
	if(dis[start] == inf) {
		return -1;
	}
	// A(f, g, id)
	que.push(A(dis[start], 0, start));
	while(!que.empty()) {
		A tmp = que.top();
		que.pop();
		if(tmp.id == end) {
			cnt++;
			if(cnt == k)
				return tmp.g;
		}
		for(int i = head[tmp.id]; i != -1; i = e[i].nxt) {
			// f = tmp.g + dis[tmp.id] + e[i].w
			// g = tmp.g + e[i].w;
			// id = e[i].v;
			int id = e[i].v;
			int g = tmp.g + e[i].w; // 真实值 = 前一个点的真实值 + 到这个点的花费 
			int f = g + dis[id];	// 总值 = 真实值 + 估计值 
			que.push(A(f, g, id));
		}
	}
	return -1;
}

int main() {
	while(scanf("%d %d", &n, &m) != EOF) {
		init();
		scanf("%d %d %d %d", &st, &ed, &k, &t);
		int u,v,w;
		for(int i = 1; i <= m; i++) {
			scanf("%d %d %d", &u, &v, &w);
			add(u,v,w); 
		}
		spfa(ed);
		int ans = A_Star(st,ed);
		if(ans <= t && ans != -1){
			puts("yareyaredawa");
		}else{
			puts("Whitesnake!");
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值