Made In Heaven

题目描述

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.

输入

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, Kand 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.

输出

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*算法及模板来自https://blog.csdn.net/z_mendez/article/details/47057461

代码

#include<bits/stdc++.h>
using namespace std;
#define INF 0xffffff
#define MAXN 100010
struct node
{
int to;
int val;
int next;
};
struct node2
{
int to;
int g,f;
bool operator<(const node2 &r ) const
{
if(r.f==f)
return r.g<g;
return r.f<f;
}
};
node edge[MAXN],edge2[MAXN];
int n,m,s,t,k,T,cnt,cnt2,ans;
int dis[1010],visit[1010],head[1010],head2[1010];
void init()
{
memset(head2,-1,sizeof(head2));
memset(head,-1,sizeof(head));
cnt=cnt2=1;
}
void addedge(int from,int to,int val)
{
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
void addedge2(int from,int to,int val)
{
edge2[cnt2].to=to;
edge2[cnt2].val=val;
edge2[cnt2].next=head2[from];
head2[from]=cnt2++;
}
bool spfa(int s,int n,int head[],node edge[],int dist[])
{
queue<int>Q1;
int inq[1010];
for(int i=0;i<=n;i++)
{
dis[i]=INF;
inq[i]=0;
}
dis[s]=0;
Q1.push(s);
inq[s]++;
while(!Q1.empty())
{
int q=Q1.front();
Q1.pop();
inq[q]--;
if(inq[q]>n)
return false;
int k=head[q];
while(k>=0)
{
if(dist[edge[k].to]>dist[q]+edge[k].val)
{
dist[edge[k].to]=edge[k].val+dist[q];
if(!inq[edge[k].to])
{
inq[edge[k].to]++;
Q1.push(edge[k].to);
}
}
k=edge[k].next;
}
}
return true;
}
int A_star(int s,int t,int n,int k,int head[],node edge[],int dist[])
{
node2 e,ne;
int cnt=0;
priority_queue<node2>Q;
if(s==t)
k++;
if(dis[s]==INF)
return -1;
e.to=s;
e.g=0;
e.f=e.g+dis[e.to];
Q.push(e); while(!Q.empty())
{
e=Q.top();
Q.pop();
if(e.to==t)//找到一条最短路径
{
cnt++;
}
if(cnt==k)//找到k短路
{
return e.g;
}
for(int i=head[e.to]; i!=-1; i=edge[i].next)
{
ne.to=edge[i].to;
ne.g=e.g+edge[i].val;
ne.f=ne.g+dis[ne.to];
Q.push(ne);
}
}
return -1; // 找不到
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
scanf("%d%d%d%d",&s,&t,&k,&T);
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge2(b,a,c);
}
spfa(t,n,head2,edge2,dis);
ans=A_star(s,t,n,k,head,edge,dis);
//printf("%d\n",ans);
if(ans==-1||ans > T) printf("Whitesnake!\n");
else printf("yareyaredawa\n");
}
return 0;
}

最新文章

  1. 各大主流.Net的IOC框架性能测试比较
  2. 关于Windows窗口框架
  3. 七个结构模式之适配器模式(Adapter Pattern)
  4. Money类型转化为String去除小数点后0解决方法
  5. timer.scheduleAtFixedRate和timer.schedule的实验
  6. 如何在组件(Component中)模拟用户控件(UserControl)中FindForm()?
  7. 《深入.NET平台和C#编程》内部测试题-笔试试卷
  8. Fizz Buzz
  9. 使用非java代码编程
  10. Mysql加锁过程详解(5)-innodb 多版本并发控制原理详解
  11. ffmpeg 增加视频流媒体质量评估滤镜 (Video Multimethod Assessment Fusion, VMAF)
  12. Datatable的操作方法
  13. Tensorflow[架构流程]
  14. noip第26课作业
  15. 基于HTML5手机登录注册表单代码
  16. leetcode_目录
  17. rotate-function
  18. 使用FileSystemWatcher监视指定目录
  19. jade
  20. PHP 笔记——PDO操作数据库

热门文章

  1. poi小案例
  2. Day9---Python的集合类
  3. 关联查询总结,left join 和 inner join 区别和优化
  4. python常用函数 I
  5. 同步与异步,阻塞与非阻塞 bio,nio,aio
  6. 前端每日实战:50# 视频演示如何用纯 CSS 创作一个永动的牛顿摆
  7. 为什么我们从Angular 2迁移到Vue.js(为什么我们没有选择React)
  8. ES5和ES6数组方法
  9. pgsql SQL监控,查询SQL执行情况
  10. $emit 和 $on 进行平行组件之间的传值