CF1341E. Nastya and Unexpected Guest(01bfs)

2024-04-16 01:18

本文主要是介绍CF1341E. Nastya and Unexpected Guest(01bfs),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

If the girl doesn’t go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.

On the way from Denis’s house to the girl’s house is a road of 𝑛 lines. This road can’t be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.

Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up 𝑔 seconds green, and then 𝑟 seconds red, then again 𝑔 seconds green and so on.

Formally, the road can be represented as a segment [0,𝑛]. Initially, Denis is at point 0. His task is to get to point 𝑛 in the shortest possible time.

He knows many different integers 𝑑1,𝑑2,…,𝑑𝑚, where 0≤𝑑𝑖≤𝑛 — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.

Unfortunately, Denis isn’t always able to control himself because of the excitement, so some restrictions are imposed:

He must always move while the green light is on because it’s difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ±1 in 1 second. While doing so, he must always stay inside the segment [0,𝑛].
He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ±1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by −1, on a safety island he can change position by ±1, and at any other point by −1.
At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to 𝑛.

This task was not so simple, because it’s possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn’t solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.

Input
The first line contains two integers 𝑛 and 𝑚 (1≤𝑛≤106,2≤𝑚≤𝑚𝑖𝑛(𝑛+1,104)) — road width and the number of safety islands.

The second line contains 𝑚 distinct integers 𝑑1,𝑑2,…,𝑑𝑚 (0≤𝑑𝑖≤𝑛) — the points where the safety islands are located. It is guaranteed that there are 0 and 𝑛 among them.

The third line contains two integers 𝑔,𝑟 (1≤𝑔,𝑟≤1000) — the time that the green light stays on and the time that the red light stays on.

Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.

If it is impossible to cross the road output −1.

Examples
inputCopy
15 5
0 3 7 14 15
11 11
outputCopy
45
inputCopy
13 4
0 3 7 13
9 9
outputCopy
-1
Note
In the first test, the optimal route is:

for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
for the second green light reaches 14. Wait for the red light again.
for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.

In the second test, it is impossible to cross the road according to all the rules.

题意:
一条路长度为 n n n,从0出发。有 m m m个站点。
绿灯红灯交替变换。绿灯的时候你必须走,且从一个站点出来后方向是确定的。
红灯的时候必须在一个站点上。求最短多少时间可以到达 n n n点。

思路:
可以算作广义的最短路。
定义 d [ i ] [ j ] d[i][j] d[i][j]为到了第 i i i个站点时,绿灯当前时间为 j j j,经过了多少轮红绿灯变换。
如果你定义 d [ i ] [ j ] d[i][j] d[i][j]为到了这个状态的最短时间,那么很明显就是求一个最短路了。

但是官方题解的定义好处在于,转移中唯一有权值的边为 d [ i ] [ g ] − > d [ i ] [ 0 ] d[i][g]->d[i][0] d[i][g]>d[i][0],这意味着等一个红灯。这就变成了01bfs问题。意思就是权值只有0和1。这种问题的解法是边权为1的节点入队尾。边权为0的节点入队首。

所能转移到的子节点就是 d [ i − 1 ] [ k ] d[i-1][k] d[i1][k], d [ i + 1 ] [ k ] d[i+1][k] d[i+1][k], d [ i ] [ 0 ] d[i][0] d[i][0]

#include <cstdio>
#include <algorithm>
#include <map>
#include <iostream>
#include <vector>
#include <deque>using namespace std;
typedef long long ll;int n,m;
int g,r;
int d[10007][1007];
bool vis[10007][1007];
int a[10007];struct Node {int p,v;
};ll bfs() {deque<Node>q;q.push_back({0,0});vis[0][0] = 1;ll ans = -1;while(!q.empty()) {Node now = q.front();q.pop_front();if(now.v == 0) { //可以证明当一个点可以直接走到终点的时候,其肯定是一个绿灯时间为0的点走过来的,所以我们只判断now.v==0的情况int rem = n - a[now.p]; //你也可以对所有点进行判断。if(rem <= g) {ll tmp = 1ll * d[now.p][now.v] * (g + r) + rem;if(ans == -1 || tmp < ans) {ans = tmp;}}}if(now.v == g) {if(!vis[now.p][0]) {d[now.p][0] = d[now.p][now.v] + 1;vis[now.p][0] = 1;q.push_back({now.p,0});}continue;}if(now.p > 1) {int p = now.p - 1;int v = now.v + a[now.p] - a[p];if(v <= g && !vis[p][v]) {vis[p][v] = 1;d[p][v] = d[now.p][now.v];q.push_front({p,v});}}if(now.p < m) {int p = now.p + 1;int v = now.v + a[p] - a[now.p];if(v <= g && !vis[p][v]) {vis[p][v] = 1;d[p][v] = d[now.p][now.v];q.push_front({p,v});}}}return ans;
}int main() {scanf("%d%d",&n,&m);for(int i = 1;i <= m;i++) {scanf("%d",&a[i]);}scanf("%d%d",&g,&r);sort(a + 1,a + 1 + m);printf("%lld\n",bfs());return 0;
}

这篇关于CF1341E. Nastya and Unexpected Guest(01bfs)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907443

相关文章

【NodeJS】Unexpected token (109:0) 返回错误码500

刚开始报错是这样的: Unexpected token call 是什么我没看懂,但我发现 span.label.lable-success 后面的 #[i+1] 写错了,应该是 #{i+1} 改成完这个错误后又是一个错误提示: What? Unexpected token (109:0) 返回错误码500是什么鬼 我先将自己这段源码的 - if ... - else 检查下

记一次knife4j文档请求异常 SyntaxError: Unexpected token ‘<‘, ... is not valid JSON

knife4j页面报错问题定位 前几天开发新接口,开发完成后想使用knife4j测试一下接口功能,突然发现访问页面报错提示:knife4j文档请求异常,但之前运行还是正常的,想想会不会与升级依赖有关系,启动其他微服务发现文档接口访问正常,排除因依赖版本升级导致在线API文档无法使用情况,还是和本服务新增接口有关系。 定位问题 首先f12打开调试台,重新刷新页面,看到console有报错提示

Unexpected token d in JSON at position 5, check bodyParser config错误解决

错误原因:json格式不对 { desc="设备1", iotProjectId=11 } 解决:通过json在线校验格式校验json格式,找出错误原因,修改 在线JSON校验格式化工具(Be JSON) 修改: {"desc": "设备","iotProjectId": 11}

ElementPlusError: [ElForm] unexpected width NaN 解决方法

我自己在使用 Vue 和 ElementPlus 开发项目时,当切换到某些页面时,控制台会出现如下错误: 经过分析,问题原因如下: • el-form 组件设置了 label-width=“auto”,并且该组件处于隐藏状态(例如被 display: none 隐藏,项目中是由于 el-tab 组件的切换导致的)。 • 当切换页面时,这个隐藏的表单组件会引发问题。具体来说,el-form 组

org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: 29 near line 1, column

@SuppressWarnings("unchecked")   public List<Strudent> getStudent(int count) {       String hql = "select top "+count+" from Student";       return (List<Student>)getHibernateTemplate().fin

Could not create the view: An unexpected exception was thrown

今天在打开MyEclipse的时候,出现了“Could not create the view: An unexpected exception was thrown“,然后各种运行都运行不了。   上网查了一下解决方法 1、  关闭MyEclipse 2、  找到workspace所在的目录中的“.metadata/.plugins/org.eclipse.core.runtime/.s

jedis connection exception unexpected end of stream

jedis connection exception unexpected end of stream 多线程的时候,我的代码起初是这样子的: ExecutorService pool = Executors.newFixedThreadPool(100); // 线程池100Jedis jedis = jedisPool.getResource();for (int i = 0; i <

SyntaxError: Unexpected token ‘??=‘ 解决办法

问题 原因 Node 15, 及 以上版本才能使用 ?? 操作符 我的版本: 解决 尝试升级node版本 参考 windows下node升级到最新版本(亲测有效) 有错误,但也创建成功了。 错误以后再改吧…

RESTful学习笔记 --- TypeError: __init__() got an unexpected keyword argument 'method'

最近在写restful api的时候一直报出如下的错误, 原因是因为methods参数写成了method,因此正确的写法就是加上's',写成methods 如:

JS SyntaxError: Unexpected token 报错解决

JS SyntaxError: Unexpected token 报错解决 在JavaScript开发中,SyntaxError: Unexpected token 是一个常见的错误,它通常表示JavaScript引擎在解析代码时遇到了意料之外的符号。这个错误可能由多种原因引起,包括拼写错误、缺少括号、引号不匹配等。本文将深入探讨此错误的常见原因,并提供解决思路和实战指南。