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;
}

upc组队赛15 Made In Heaven【第K短路 A*】的更多相关文章

  1. ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解

    思路:K短路裸题 代码: #include<queue> #include<cstring> #include<set> #include<map> # ...

  2. 沈阳网络赛D-Made In Heaven【k短路】【模板】

    One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci ...

  3. upc组队赛15 Lattice's basics in digital electronics【模拟】

    Lattice's basics in digital electronics 题目链接 题目描述 LATTICE is learning Digital Electronic Technology. ...

  4. upc组队赛15 Supreme Number【打表】

    Supreme Number 题目链接 题目描述 A prime number (or a prime) is a natural number greater than 1 that cannot ...

  5. ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)

    Made In Heaven One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. ...

  6. 面试题 15:链表中倒数第 k 个结点

    面试题 15:链表中倒数第 k 个结点 题目:输入一个链表,输出该链表中倒数第 k 个结点.为了符合大多数人的习惯, 本题从 1 开始计数,即链表的尾结点是倒数第一个结点.例如一个有 6 个结点的 链 ...

  7. 题目15 链表中倒数第K个节点

    ///////////////////////////////////////////////////////////////////////////////////// // 5. 题目15 链表中 ...

  8. ACM-ICPC 2018 沈阳赛区网络预赛 D Made In Heaven(第k短路,A*算法)

    https://nanti.jisuanke.com/t/31445 题意 能否在t时间内把第k短路走完. 分析 A*算法板子. #include <iostream> #include ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(第k短路模板)

    求第k短路模板 先逆向求每个点到终点的距离,再用dij算法,不会超时(虽然还没搞明白为啥... #include<iostream> #include<cstdio> #inc ...

随机推荐

  1. poi小案例

    一:pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...

  2. 1.openshift搭建

    第1章 主机规划和所需文件 1.1 主机规划 IP地址 域名 用途 11.11.233.125 master01.song.test.cnpc 容器编排.etcd 11.11.233.126 mast ...

  3. Go语言_方法和接口

    方法和接口 本节课包含了方法和接口,可以用这种构造来定义对象及其行为. Go 作者组编写,Go-zh 小组翻译. https://tour.go-zh.org/methods/1 方法 Go 没有类. ...

  4. vue 实现模糊检索,并根据其他字符的首字母顺序排列

    昨天让我做一个功能,实现一个模糊检索,我就想,那做呗,然后开始正常的开发 代码如下: HTML VUE 因为是实时的,所以写了将逻辑写到了watch中 五分钟搞定.   我以为这就完了,然而产品的需求 ...

  5. vue项目build打包后图片路径不对导致图片空白不显示问题解决方法

    1.在上篇文章src配置及引入的基础上修改项目配置: 文章链接地址:https://www.cnblogs.com/hsl-shiliang/p/10333022.html. 2.具体配置过程如图: ...

  6. qt如何去掉文件路径最后一个反斜杠的内容

    QString strTemp = “E:\\hell\\word\\detect.txt”   int nIndex = strTemp.lastIndexOf('\\');    strTemp ...

  7. ltp-ddt smp_cpu_affinity

    # @name SMP CPU Affinity# @desc Check that processes assigned to multiple CPUs complete without erro ...

  8. SpringBoot---注册Servlet,Filter,Listener

    1.概述 1.1.当使用  内嵌的Servlet容器(Tomcat.Jetty等)时,将Servlet,Filter,Listener  注册到Servlet容器的方法: 1.1.1.直接注册Bean ...

  9. Sublime Text 注册及使用相关

    sublime text3 注册码 2019-07-01 注册码可以直接用 地址: 2019-07-01 亲测可用 2019-07-18 亲测可用 -– BEGIN LICENSE -– Die So ...

  10. js 运动框架-轻量级

    具体代码如下: function move(obj,json,sv,fnEnd){ //CSS样式值 function getStyle(obj,attr){ if(obj.currentStyle) ...