ACM-ICPC2018 沈阳赛区网络预赛-D-Made In Heaven8
A*算法:
A*,启发式搜索,是一种较为有效的搜索方法。
我们在搜索的时候,很多时候在当前状态,已经不是最优解了,但是我们却继续求解;这个就是暴力搜索浪费时间的原因。
我们在有些时候,往往可以根据一些信息推断出继续搜索是一种劣解。
所以如果能够判断出来的话,就可以不继续了,以达到节省运行时间的目的。
估价函数:
为了提高搜索效率,我们可以对未来可能产生的代价进行预估。我们设计一个估价函数,以任意状态输入,计算出从该状态到目标状态所需代价的估计值。
在搜索时,我们总沿着当前代价+未来估价最小的状态进行搜索。 估价函数需要满足:
设当前状态state到目标函数所需代价的估计值为f(state)
设在未来的搜索中,实际求出的从当前状态state到目标状态的最小代价为g(state)
对于任意的state,应该有f(state)<=g(state)
也就是说,估价函数的估值不能大于未来实际代价,估价比实际代价更优。
第K短路:
根据估价函数的设计准则,在第K短路中从x到T的估计距离f(x)应该不大于第K短路中从x到T的实际距离g(x),于是,我们可以把估价函数f(x)定为从x到T的最短路径长度,这样不但能保证f(x)<=g(x),还能顺应g(x)的实际变化趋势。
实现过程:
.预处理f(x),在反向图上以T为起点求到每个点的最短路
.定义堆,维护{p,g,h},p是某一个点,g是估价,h是实际,那么g+h更小的点p会优先访问
.取出堆顶元素u扩展,如果节点v被取出的次数尚未达到k,就把新的{v,g,h+length(u,v)}插入堆中
.重复第2-3步,直到第K次取出终点T,此时走过的路径长度就是第K短路 因为估价函数的作用,图中很多节点访问次数远小于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
#include <bits/stdc++.h>
using namespace std;
const int N=;
const int M=;
inline int read()
{
int x=,f=;char ch=getchar();
while (ch<'' || ch>''){if (ch=='-') f=-;ch=getchar();}
while (ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,sta,en,kth,T,Ecnt,Eoppcnt;
int dist[N];
int times[N];
bool vis[N];
struct Edge{
int to,next,val;}E[M],Eopp[M];//Eopp means Eopposite
int last[N],last_opp[N];
struct A_Star_node{
int p,g,h;
bool operator < (A_Star_node x)const
{
return x.g+x.h<g+h;
}
};//means point and a_Star:f(x)=g(x)+h(x);
priority_queue<A_Star_node>Q;
inline void add(int u,int v,int w)
{
Ecnt++;
E[Ecnt].next=last[u];
E[Ecnt].to=v;
E[Ecnt].val=w;
last[u]=Ecnt;
}
inline void add_opposite(int u,int v,int w)
{
Eoppcnt++;
Eopp[Eoppcnt].next=last_opp[u];
Eopp[Eoppcnt].to=v;
Eopp[Eoppcnt].val=w;
last_opp[u]=Eoppcnt;
}
void dij(int s,int e)
{
memset(vis,,sizeof(vis));
memset(dist,,sizeof(dist));
int mi;
dist[e]=;
for (int i=;i<=n;i++)
{
mi=;
for (int j=;j<=n;j++)
if (!vis[j] && dist[mi]>dist[j]) mi=j;
vis[mi]=;
for (int x=last_opp[mi];x;x=Eopp[x].next)
dist[Eopp[x].to]=min(dist[Eopp[x].to],dist[mi]+Eopp[x].val);
}
}
int A_Star(int s,int e)
{
A_Star_node t1,tmp;
memset(times,,sizeof(times));
while (!Q.empty()) Q.pop();
t1.g=t1.h=; t1.p=s;
Q.push(t1);
while (!Q.empty())
{
t1=Q.top(); Q.pop();
times[t1.p]++;
if (times[t1.p]<=kth && t1.h>T) return -;//K短路之前的路已经比要求的时间长了,一定是不合法的方案
if (times[t1.p]==kth && t1.p==e) return t1.h;
if (times[t1.p]>kth) continue;
for (int i=last[t1.p];i;i=E[i].next)
{
tmp.p=E[i].to;
tmp.g=dist[E[i].to];
tmp.h=E[i].val+t1.h;
Q.push(tmp);
}
}
return -;
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
sta=read(); en=read(); kth=read(); T=read();
int x,y,z;
memset(last,,sizeof(last));
memset(last_opp,,sizeof(last_opp));
Ecnt=;
Eoppcnt=;
while (m--)
{
x=read(); y=read(); z=read();
add(x,y,z);
add_opposite(y,x,z);
}
dij(sta,en);
if (sta==en) kth++;
int ans=A_Star(sta,en);
if (ans==-||ans>T) puts("Whitesnake!");
else puts("yareyaredawa");
}
return ;
}
ACM-ICPC2018 沈阳赛区网络预赛-D-Made In Heaven8的更多相关文章
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number
Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...
- 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. ...
- 图上两点之间的第k最短路径的长度 ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven
131072K One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. Howe ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J树分块
J. Ka Chang Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero p ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number
A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)
https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...
- ACM-ICPC 2018 沈阳赛区网络预赛 B Call of Accepted(表达式求值)
https://nanti.jisuanke.com/t/31443 题意 给出一个表达式,求最小值和最大值. 表达式中的运算符只有'+'.'-'.'*'.'d',xdy 表示一个 y 面的骰子 ro ...
- ACM-ICPC 2018 沈阳赛区网络预赛 G Spare Tire(容斥)
https://nanti.jisuanke.com/t/31448 题意 已知a序列,给你一个n和m求小于n与m互质的数作为a序列的下标的和 分析 打表发现ai=i*(i+1). 易得前n项和为 S ...
随机推荐
- World Cup(思维+模拟)
Description Allen wants to enter a fan zone(球迷区) that occupies a round square and has nn entrances. ...
- c# richBox内容转图片
1.自定义控件,继承richBox public class RichTextBoxPrintCtrl : RichTextBox { //private const double anInch = ...
- “我爱淘”第二冲刺阶段Scrum站立会议6
完成任务: 完成学院分类的点击查看书籍功能,可以点击书的条目查看书的详细信息. 计划任务: 将书的详细信息进行完善,并且可以点击收藏以及已预订等功能. 遇到问题: 分类功能,根据不同学院,自动将数据库 ...
- 寒假MOOC学习计划
我选择的是西北工业大学的课程,理由如下: 首先,选择这门课的网友还蛮多的,特意看了一下评价,也不错: 其次,这个课程的排版与我从图书馆借来的一本书内容排版比较符合,可以结合起来一起看,说不定会有更多收 ...
- Windows Forms编程实战学习:第二章 欢迎使用Visual Studio
第二章 欢迎使用Visual Studio 1,AssemblyInfo文件 包含程序集的属性,向应用程序添加元数据 [assembly:<attribute>(<setting&g ...
- 03_Java基础语法_第3天(Scanner、Random、流程控制语句)_讲义
今日内容介绍 1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scan ...
- webpack打包多html开发案例新
闲来无事在原来简单打包案例的基础上,参考vue-cli的打包代码,改为多文件打包. 区别于上篇文章<webpack打包多html开发案例>,此次打包根据开发的不同环节进行打包,也就是有开发 ...
- 图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目
第 1 部分:新建一个 Java Web Application 项目 File -> New -> Project…,请选择 Java EE 这个模块下的 Web Application ...
- Docker 入门 到部署Web 程序- (阿里面试常用的docker命令和优点)
最近阿里的面试官问我Docker是做什么用的,我记得之前360和美团,京东的都问过,但是一直没时间看,最近有时间了,系统的学习了一下Docker,在此做一下记录,方便各位看官学习交流 一.Docker ...
- 【操作系统、UNIX环境编程】进程间通信
多个进程可以共享系统中的各种资源,但其中许多资源一次只能为一个进程使用,我们把一次仅允许一个进程使用的资源称为临界资源,许多物理设备都属于临界资源,如打印机等. Linux下进程间通信有如下几种方式: ...