题目链接:路虽远

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

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

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

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

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

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

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

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

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

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

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

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

  1. 转移点

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

  3. 闯黄灯的次数

  4. 最短路时间

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

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

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

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

参照代码
  1. #include <bits/stdc++.h>
  2. //#pragma GCC optimize("Ofast,unroll-loops")
  3. #define isPbdsFile
  4. #ifdef isPbdsFile
  5. #include <bits/extc++.h>
  6. #else
  7. #include <ext/pb_ds/priority_queue.hpp>
  8. #include <ext/pb_ds/hash_policy.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. #include <ext/pb_ds/trie_policy.hpp>
  11. #include <ext/pb_ds/tag_and_trait.hpp>
  12. #include <ext/pb_ds/hash_policy.hpp>
  13. #include <ext/pb_ds/list_update_policy.hpp>
  14. #include <ext/pb_ds/assoc_container.hpp>
  15. #include <ext/pb_ds/exception.hpp>
  16. #include <ext/rope>
  17. #endif
  18. using namespace std;
  19. using namespace __gnu_cxx;
  20. using namespace __gnu_pbds;
  21. typedef long long ll;
  22. typedef long double ld;
  23. typedef pair<int, int> pii;
  24. typedef pair<ll, ll> pll;
  25. typedef tuple<int, int, int> tii;
  26. typedef tuple<ll, ll, ll> tll;
  27. typedef unsigned int ui;
  28. typedef unsigned long long ull;
  29. typedef __int128 i128;
  30. #define hash1 unordered_map
  31. #define hash2 gp_hash_table
  32. #define hash3 cc_hash_table
  33. #define stdHeap std::priority_queue
  34. #define pbdsHeap __gnu_pbds::priority_queue
  35. #define sortArr(a, n) sort(a+1,a+n+1)
  36. #define all(v) v.begin(),v.end()
  37. #define yes cout<<"YES"
  38. #define no cout<<"NO"
  39. #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  40. #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
  41. #define forn(i, a, b) for(int i = a; i <= b; i++)
  42. #define forv(i, a, b) for(int i=a;i>=b;i--)
  43. #define ls(x) (x<<1)
  44. #define rs(x) (x<<1|1)
  45. #define endl '\n'
  46. //用于Miller-Rabin
  47. [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
  48. template <typename T>
  49. int disc(T* a, int n)
  50. {
  51. return unique(a + 1, a + n + 1) - (a + 1);
  52. }
  53. template <typename T>
  54. T lowBit(T x)
  55. {
  56. return x & -x;
  57. }
  58. template <typename T>
  59. T Rand(T l, T r)
  60. {
  61. static mt19937 Rand(time(nullptr));
  62. uniform_int_distribution<T> dis(l, r);
  63. return dis(Rand);
  64. }
  65. template <typename T1, typename T2>
  66. T1 modt(T1 a, T2 b)
  67. {
  68. return (a % b + b) % b;
  69. }
  70. template <typename T1, typename T2, typename T3>
  71. T1 qPow(T1 a, T2 b, T3 c)
  72. {
  73. a %= c;
  74. T1 ans = 1;
  75. for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
  76. return modt(ans, c);
  77. }
  78. template <typename T>
  79. void read(T& x)
  80. {
  81. x = 0;
  82. T sign = 1;
  83. char ch = getchar();
  84. while (!isdigit(ch))
  85. {
  86. if (ch == '-')sign = -1;
  87. ch = getchar();
  88. }
  89. while (isdigit(ch))
  90. {
  91. x = (x << 3) + (x << 1) + (ch ^ 48);
  92. ch = getchar();
  93. }
  94. x *= sign;
  95. }
  96. template <typename T, typename... U>
  97. void read(T& x, U&... y)
  98. {
  99. read(x);
  100. read(y...);
  101. }
  102. template <typename T>
  103. void write(T x)
  104. {
  105. if (typeid(x) == typeid(char))return;
  106. if (x < 0)x = -x, putchar('-');
  107. if (x > 9)write(x / 10);
  108. putchar(x % 10 ^ 48);
  109. }
  110. template <typename C, typename T, typename... U>
  111. void write(C c, T x, U... y)
  112. {
  113. write(x), putchar(c);
  114. write(c, y...);
  115. }
  116. template <typename T11, typename T22, typename T33>
  117. struct T3
  118. {
  119. T11 one;
  120. T22 tow;
  121. T33 three;
  122. bool operator<(const T3 other) const
  123. {
  124. if (one == other.one)
  125. {
  126. if (tow == other.tow)return three < other.three;
  127. return tow < other.tow;
  128. }
  129. return one < other.one;
  130. }
  131. T3() { one = tow = three = 0; }
  132. T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
  133. {
  134. }
  135. };
  136. template <typename T1, typename T2>
  137. void uMax(T1& x, T2 y)
  138. {
  139. if (x < y)x = y;
  140. }
  141. template <typename T1, typename T2>
  142. void uMin(T1& x, T2 y)
  143. {
  144. if (x > y)x = y;
  145. }
  146. constexpr ll INF = 1e16 + 10;
  147. constexpr int N = 110;
  148. ll dist[N][N][N]; //终点、选择不限速边数、闯黄灯次数
  149. int n, m, k, g;
  150. ll green[N], yellow[N], red[N], all[N]; //all为一个周期的时间
  151. struct Status
  152. {
  153. int pos, k_cnt, g_cnt;
  154. ll min_time; //终点,选择不限边数,闯黄灯次数,最短时间
  155. bool operator<(const Status& other) const
  156. {
  157. return min_time > other.min_time; //反过来保证最小堆
  158. }
  159. };
  160. vector<tll> child[N]; //相邻点、非限速时间、限速时间
  161. stdHeap<Status> q;
  162. inline void update(const int pos, const int k_cnt, const int g_cnt, const ll min_time)
  163. {
  164. ll& tmp = dist[pos][k_cnt][g_cnt];
  165. if (min_time < tmp)tmp = min_time, q.emplace(pos, k_cnt, g_cnt, min_time);
  166. }
  167. enum Color
  168. {
  169. Green,
  170. Yello,
  171. Red
  172. };
  173. inline Color getColor(const ll tim, const int pos)
  174. {
  175. if (tim < green[pos])return Green;
  176. if (tim < green[pos] + yellow[pos])return Yello;
  177. return Red;
  178. }
  179. inline void dijkstra()
  180. {
  181. forn(i, 1, n)forn(j, 0, k)forn(s, 0, g)dist[i][j][s] = INF;
  182. dist[1][0][0] = 0;
  183. q.emplace(1, 0, 0, 0);
  184. while (!q.empty())
  185. {
  186. auto [pos, k_cnt, g_cnt, min_time] = q.top();
  187. q.pop();
  188. ll rem = min_time % all[pos]; //处在过马路这个点时的红绿灯剩余时间
  189. if (dist[pos][k_cnt][g_cnt] < min_time)continue; //重复状态量进行剪枝,防止退化
  190. for (const auto [nxt_pos,t1,t2] : child[pos]) //t1为不限速,t2为限速
  191. {
  192. Color curr = getColor(rem, pos);
  193. int need = curr != Green ? all[pos] - rem : 0; //还需要到绿灯的时间
  194. //限速与不限速,闯黄灯与不闯黄灯,四种组合
  195. if (k_cnt < k)
  196. {
  197. //限速
  198. //闯黄灯
  199. if (curr == Yello and g_cnt < g)update(nxt_pos, k_cnt + 1, g_cnt + 1, min_time + t1);
  200. //不闯黄灯
  201. update(nxt_pos, k_cnt + 1, g_cnt, min_time + need + t1);
  202. }
  203. //不限速
  204. //闯黄灯
  205. if (curr == Yello and g_cnt < g)update(nxt_pos, k_cnt, g_cnt + 1, min_time + t2);
  206. //不闯黄灯
  207. update(nxt_pos, k_cnt, g_cnt, min_time + need + t2);
  208. }
  209. }
  210. }
  211. inline void solve()
  212. {
  213. cin >> n >> m >> k >> g;
  214. k = m - k; //转化为可以选择不限速的边,可以更好地贪心
  215. forn(i, 1, n)cin >> green[i] >> yellow[i] >> red[i], all[i] = green[i] + yellow[i] + red[i];
  216. forn(i, 1, m)
  217. {
  218. int u, v, t1, t2;
  219. cin >> u >> v >> t1 >> t2;
  220. child[u].emplace_back(v, t1, t2);
  221. child[v].emplace_back(u, t1, t2);
  222. }
  223. dijkstra();
  224. ll ans = INF;
  225. forn(i, 0, k)forn(j, 0, g)uMin(ans, dist[n][i][j]);
  226. cout << (ans == INF ? -1 : ans);
  227. }
  228. signed int main()
  229. {
  230. Spider
  231. //------------------------------------------------------
  232. int test = 1;
  233. // read(test);
  234. // cin >> test;
  235. forn(i, 1, test)solve();
  236. // while (cin >> n, n)solve();
  237. // while (cin >> test)solve();
  238. }
\[最终复杂度为:O(k \times g \times m \log{(k \times g \times n)})
\]

P9549 「PHOI-1」路虽远 题解的更多相关文章

  1. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  2. NBL小可爱纪念赛「 第一弹 」 游记(部分题解)

    比赛链接 洛谷:禁止含有侮辱性质的比赛 . ??? 反正我觉得,gyx挺危险的 不说废话. 首先,比赛经验,前几个小时不打,跟着刷榜. 一看 T1. 发现是道水题,直接切掉了. 然后看到了 T2. 感 ...

  3. LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)

    写在这道题前面 : 网上的一些题解都不讲那个系数是怎么推得真的不良心 TAT (不是每个人都有那么厉害啊 , 我好菜啊) 而且 LOJ 过的代码千篇一律 ... 那个系数根本看不出来是什么啊 TAT ...

  4. LOJ #2538. 「PKUWC 2018」Slay the Spire (期望dp)

    Update on 1.5 学了 zhou888 的写法,真是又短又快. 并且空间是 \(O(n)\) 的,速度十分优秀. 题意 LOJ #2538. 「PKUWC 2018」Slay the Spi ...

  5. 「JOI2019 Final」解题报告

    传送门 「JOI2019 Final」勇者比太郎 看懂题就很简单了,后缀和随便维护一下就好了,别用树状数组强加一个\(\log\)就行. 「JOI2019 Final」画展 显然可以先把所有的画框按大 ...

  6. 前端构建工具之gulp(一)「图片压缩」

    前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...

  7. fir.im Weekly - 如何打造 Github 「爆款」开源项目

    最近 Android 转用 Swift 的传闻甚嚣尘上,Swift 的 Github 主页上已经有了一次 merge>>「Port to Android」,让我们对 Swift 的想象又多 ...

  8. 「前端开发者」如何把握住「微信小程序」这波红利?

    由于前两周一直在老家处理重要事情,虽然朋友圈被「微信小程序」刷爆了,但并没有时间深入了解. 昨天回广州之后,第一件事情就是把「微信小程序」相关的文章.开发文档.设计规范全部看了一遍,基本上明白了「微信 ...

  9. 微服务架构之「 API网关 」

    在微服务架构的系列文章中,前面已经通过文章<架构设计之「服务注册 」>介绍过了服务注册的原理和应用,今天这篇文章我们来聊一聊「 API网关 」. 「 API网关 」是任何微服务架构的重要组 ...

  10. 从0开始学习 GITHUB 系列之「加入 GITHUB」【转】

    本文转载自:http://stormzhang.com/github/2016/05/26/learn-github-from-zero2/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...

随机推荐

  1. vue.draggable中文文档

    http://www.itxst.com/vue-draggable/fiamvqam.html https://blog.csdn.net/a772116804/article/details/10 ...

  2. Python异步编程原理篇之协程的IO

    协程的IO asyncio 作为实现异步编程的库,任务执行中遇到系统IO的时能够自动切换到其他任务.协程使用的IO模型是IO多路复用.在 asyncio 低阶API 一篇中提到过 "以Lin ...

  3. sublime_text4 2023最新版 激活教程

    官网 Sublime HQ - Remarkable Software 东西在教学的时候还是挺好用的,就是要付费购买,穷,没钱 买不起,自己动手丰衣足食. 下载安装包 我现在最新版是4.4152 下面 ...

  4. java进阶(14)--日期时间处理

    一.获取系统当前时间: 1.Date(),精确到毫秒的当前当前时间 2.示例,欧美风格时间格式

  5. centos7_Lnmp编译安装

    17年面试运维岗位的时候,面试官要求输出一份lnmp编译的操作文档,于是有了如下安装nginx+php+mysql,进入正题: 准备环境 环境:centos7.3 软件:nginx-1.12.1 + ...

  6. 【MicroPython】生成micropython版本头文件 - py\makeversionhdr.py

    用法 $ python makeversionhdr.py mpversion.h 实现 带git仓 get_version_info_from_git 使用git指令:   git describe ...

  7. 2023年江苏“领航杯”MISC一个很有意思的题目(别把鸡蛋放在同一个篮子里面)

    别把鸡蛋放在同一个篮子里面 题目附件:https://wwzl.lanzoue.com/i6HmX16finnc 1.题目信息 解压压缩包打开附件,获得5141个txt文档,每个文档都有内容,发现是b ...

  8. [转帖]shell编程:变量的数值计算实践(五)

    https://www.cnblogs.com/luoahong/articles/9224495.html 算术运算符 变量的数值(整数)计算   1)(())用法:(此方法很常用)** 范例:sh ...

  9. [转帖]Linux进程的Uninterruptible sleep(D)状态

    https://cloud.tencent.com/developer/article/1581022 Linux系统进程状态 PROCESS STATE CODES Here are the dif ...

  10. [转帖]kafka指定topic设置消息留存时间

    背景 单个主题消息量庞大,需要指定这个主题的消息留存时间缩小点 执行命令 ./bin/kafka-configs.sh --bootstrap-server node1:9092 --entity-t ...