码迷,mamicode.com
首页 > 其他好文 > 详细

CF1340C Nastya and Unexpected Guest

时间:2020-06-06 09:15:30      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:安全   限制   mat   empty   second   长度   pac   sort   pop   

题目

给你一条长度为 \(n\) 的马路(可以将马路视为一个数轴),你要从 0 位置开始到达 \(n\) 位置,你每秒走 1 个长度单位。在马路上有 \(m\) 个安全岛,它们的位置已给定。该马路的绿灯亮 \(g\) 秒,红灯亮 \(r\) 秒,第 0 秒时信号灯刚由红灯变为绿灯。

绿灯亮时,你必须一直向前走,到达一个安全岛时,你可以选择调头。红灯亮时,你必须一直停在某个安全岛处,直到绿灯再次亮起。求你最快需要多少秒能从马路的 0 位置到达 \(n\) 位置(无法到达则输出 -1)。

数据范围

\(n \le 10^6\)

\(m \le 10^4\)

\(g,r \le 10^3\)

限制

时间:1s

空间:256M

代码

# include<bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;
const int MAX1 = 1e4 + 5;
const int MAX2 = 1e3 + 5;
const int INF = 2.1e9;

int n, m, t1, t2;
int a[MAX1];
int dis[MAX1][MAX2];
deque<pii> q;

void update(int x1, int y1, int x2, int y2, int w)
{
    if (x1 > 0 && x1 <= m && y1 <= t1 && dis[x2][y2] + w < dis[x1][y1])
    {
        dis[x1][y1] = dis[x2][y2] + w;
        if (w)
        {
            q.push_back({x1, y1});
        }
        else
        {
            q.push_front({x1, y1});
        }
    }
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d", &a[i]);
    }
    scanf("%d %d", &t1, &t2);
    sort(a + 1, a + m + 1);

    for (int i = 1; i <= m; ++i)
    {
        for (int j = 0; j <= t1; ++j)
        {
            dis[i][j] = INF;
        }
    }
    dis[1][0] = 0;
    q.push_back({1, 0});

    while (!q.empty())
    {
        pii p = q.front();
        q.pop_front();
        if (p.second == t1)
        {
            update(p.first, 0, p.first, p.second, 1);
        }
        else
        {
            update(p.first - 1, p.second + (a[p.first] - a[p.first - 1]), p.first, p.second, 0);
            update(p.first + 1, p.second + (a[p.first + 1] - a[p.first]), p.first, p.second, 0);
        }
    }

    int ans = INF;
    for (int i = 0; i <= t1; ++i)
    {
        if (dis[m][i] != INF)
        {
            ans = min(ans, (t1 + t2) * dis[m][i] + i);
        }
    }
    printf("%d", ans == INF ? -1 : ans);

    return 0;
}

CF1340C Nastya and Unexpected Guest

标签:安全   限制   mat   empty   second   长度   pac   sort   pop   

原文地址:https://www.cnblogs.com/Handlip/p/13053114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!