P9549 「PHOI-1」路虽远 题解

发布时间 2024-01-11 00:02:51作者: Athanasy

题目链接:路虽远

带限制的 dijkstra,优先考虑有哪些限制条件,当做类似 dp 去写。闯黄灯次数有要求,限制速度的边数量有要求。

我们注意到,如果选择哪些边限速不易于基于贪心选择,可以考虑转换下,边数 \(-\) 限制数即为可以不限速的边,选择不限速的贪心优于限速的,这样一来,我们在有机会选择不限速的条件下,就可以考虑是否选它了。否则,就是有机会选择限速的条件下,我们无法确定该不该选这条边作为限速,因为不选才是最优的,但很显然,直接不选并不是合理的贪心,所以可以考虑如上转换。这样一来我们就可以贪心地分类讨论了。

很显然两类限制,每类限制有选与不选,一共四种组合,我们需要分四类讨论:

  1. 选择不限速 \(+\) 闯黄灯

  2. 选择不限速 \(+\) 不闯黄灯

  3. 选择限速 \(+\) 闯黄灯

  4. 选择限速 \(+\) 不闯黄灯

我们注意到关于闯黄灯的贪心:

  1. 处于绿灯,直接过马路,不用考虑是否闯黄灯。

  2. 处于黄灯,可以考虑闯黄灯更短时间到达。

  3. 处于红灯,考虑等到绿灯以后变为情况 \(1\)

\(dijkstra\) 的状态量进行封装包括有

  1. 转移点

  2. 使用了的非限速边次数

  3. 闯黄灯的次数

  4. 最短路时间

对当前时间点的颜色我们注意到:

注意到每个绿、黄、红为一个周期,我们可以找到当前位置

\[x \ 对 \ green_i+yellow_i+red_i 的余数,然后就能判断它处于什么颜色的灯了 \]

可以考虑使用枚举作为设计颜色的工具,剩余注意下爆精度的问题即可。

参照代码
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr ll INF = 1e16 + 10;
constexpr int N = 110;
ll dist[N][N][N]; //终点、选择不限速边数、闯黄灯次数
int n, m, k, g;
ll green[N], yellow[N], red[N], all[N];//all为一个周期的时间

struct Status
{
    int pos, k_cnt, g_cnt;
    ll min_time; //终点,选择不限边数,闯黄灯次数,最短时间

    bool operator<(const Status& other) const
    {
        return min_time > other.min_time; //反过来保证最小堆
    }
};

vector<tll> child[N]; //相邻点、非限速时间、限速时间
stdHeap<Status> q;

inline void update(const int pos, const int k_cnt, const int g_cnt, const ll min_time)
{
    ll& tmp = dist[pos][k_cnt][g_cnt];
    if (min_time < tmp)tmp = min_time, q.emplace(pos, k_cnt, g_cnt, min_time);
}

enum Color
{
    Green,
    Yellow,
    Red
};
//判断当前是啥颜色的灯
inline Color getColor(const ll tim, const int pos)
{
    if (tim <= green[pos])return Green;
    if (tim <= green[pos] + yellow[pos])return Yellow;
    return Red;
}

inline void dijkstra()
{
    forn(i, 1, n)forn(j, 0, k)forn(s, 0, g)dist[i][j][s] = INF;
    dist[1][0][0] = 0;
    q.emplace(1, 0, 0, 0);
    while (!q.empty())
    {
        auto [pos, k_cnt, g_cnt, min_time] = q.top();
        q.pop();
        ll rem = min_time % all[pos]; //处在过马路这个点时的红绿灯剩余时间
        if (dist[pos][k_cnt][g_cnt] > min_time)continue; //重复状态量进行剪枝,防止退化
        for (const auto [nxt_pos,t1,t2] : child[pos])//t1为不限速,t2为限速
        {
            Color curr = getColor(rem, pos);//当前颜色
            int need = curr != Green ? all[pos] - rem : 0; //还需要到绿灯的时间
            //限速与不限速,闯黄灯与不闯黄灯,四种组合
            if (k_cnt < k)
            {
                //限速
                //闯黄灯
                if (curr == Yellow and g_cnt < g)update(nxt_pos, k_cnt + 1, g_cnt + 1, min_time + t1);
                //不闯黄灯
                update(nxt_pos, k_cnt + 1, g_cnt, min_time + need + t1);
            }
            //不限速
            //闯黄灯
            if (curr == Yellow and g_cnt < g)update(nxt_pos, k_cnt, g_cnt + 1, min_time + t2);
            //不闯黄灯
            update(nxt_pos, k_cnt, g_cnt, min_time + need + t2);
        }
    }
}

inline void solve()
{
    cin >> n >> m >> k >> g;
    k = m - k; //转化为可以选择不限速的边,可以更好地贪心
    forn(i, 1, n)cin >> green[i] >> yellow[i] >> red[i], all[i] = green[i] + yellow[i] + red[i];
    forn(i, 1, m)
    {
        int u, v, t1, t2;
        cin >> u >> v >> t1 >> t2;
        child[u].emplace_back(v, t1, t2);
        child[v].emplace_back(u, t1, t2);
    }
    dijkstra();
    ll ans = INF;
    forn(i, 0, k)forn(j, 0, g)uMin(ans, dist[n][i][j]);
    cout << (ans == INF ? -1 : ans);
}

signed int main()
{
    Spider
    //------------------------------------------------------
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
}

\[最终复杂度为:O(k \times g \times m \log{(k \times g \times n)}) \]