P9549 「PHOI-1」路虽远 题解
题目链接:路虽远
带限制的 dijkstra,优先考虑有哪些限制条件,当做类似 dp 去写。闯黄灯次数有要求,限制速度的边数量有要求。
我们注意到,如果选择哪些边限速不易于基于贪心选择,可以考虑转换下,边数 \(-\) 限制数即为可以不限速的边,选择不限速的贪心优于限速的,这样一来,我们在有机会选择不限速的条件下,就可以考虑是否选它了。否则,就是有机会选择限速的条件下,我们无法确定该不该选这条边作为限速,因为不选才是最优的,但很显然,直接不选并不是合理的贪心,所以可以考虑如上转换。这样一来我们就可以贪心地分类讨论了。
很显然两类限制,每类限制有选与不选,一共四种组合,我们需要分四类讨论:
选择不限速 \(+\) 闯黄灯
选择不限速 \(+\) 不闯黄灯
选择限速 \(+\) 闯黄灯
选择限速 \(+\) 不闯黄灯
我们注意到关于闯黄灯的贪心:
处于绿灯,直接过马路,不用考虑是否闯黄灯。
处于黄灯,可以考虑闯黄灯更短时间到达。
处于红灯,考虑等到绿灯以后变为情况 \(1\)。
对 \(dijkstra\) 的状态量进行封装包括有
转移点
使用了的非限速边次数
闯黄灯的次数
最短路时间
对当前时间点的颜色我们注意到:
注意到每个绿、黄、红为一个周期,我们可以找到当前位置
\]
可以考虑使用枚举作为设计颜色的工具,剩余注意下爆精度的问题即可。
参照代码
#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,
Yello,
Red
};
inline Color getColor(const ll tim, const int pos)
{
if (tim < green[pos])return Green;
if (tim < green[pos] + yellow[pos])return Yello;
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 == Yello 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 == Yello 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();
}
\]
P9549 「PHOI-1」路虽远 题解的更多相关文章
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
- NBL小可爱纪念赛「 第一弹 」 游记(部分题解)
比赛链接 洛谷:禁止含有侮辱性质的比赛 . ??? 反正我觉得,gyx挺危险的 不说废话. 首先,比赛经验,前几个小时不打,跟着刷榜. 一看 T1. 发现是道水题,直接切掉了. 然后看到了 T2. 感 ...
- LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)
写在这道题前面 : 网上的一些题解都不讲那个系数是怎么推得真的不良心 TAT (不是每个人都有那么厉害啊 , 我好菜啊) 而且 LOJ 过的代码千篇一律 ... 那个系数根本看不出来是什么啊 TAT ...
- LOJ #2538. 「PKUWC 2018」Slay the Spire (期望dp)
Update on 1.5 学了 zhou888 的写法,真是又短又快. 并且空间是 \(O(n)\) 的,速度十分优秀. 题意 LOJ #2538. 「PKUWC 2018」Slay the Spi ...
- 「JOI2019 Final」解题报告
传送门 「JOI2019 Final」勇者比太郎 看懂题就很简单了,后缀和随便维护一下就好了,别用树状数组强加一个\(\log\)就行. 「JOI2019 Final」画展 显然可以先把所有的画框按大 ...
- 前端构建工具之gulp(一)「图片压缩」
前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...
- fir.im Weekly - 如何打造 Github 「爆款」开源项目
最近 Android 转用 Swift 的传闻甚嚣尘上,Swift 的 Github 主页上已经有了一次 merge>>「Port to Android」,让我们对 Swift 的想象又多 ...
- 「前端开发者」如何把握住「微信小程序」这波红利?
由于前两周一直在老家处理重要事情,虽然朋友圈被「微信小程序」刷爆了,但并没有时间深入了解. 昨天回广州之后,第一件事情就是把「微信小程序」相关的文章.开发文档.设计规范全部看了一遍,基本上明白了「微信 ...
- 微服务架构之「 API网关 」
在微服务架构的系列文章中,前面已经通过文章<架构设计之「服务注册 」>介绍过了服务注册的原理和应用,今天这篇文章我们来聊一聊「 API网关 」. 「 API网关 」是任何微服务架构的重要组 ...
- 从0开始学习 GITHUB 系列之「加入 GITHUB」【转】
本文转载自:http://stormzhang.com/github/2016/05/26/learn-github-from-zero2/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...
随机推荐
- vue.draggable中文文档
http://www.itxst.com/vue-draggable/fiamvqam.html https://blog.csdn.net/a772116804/article/details/10 ...
- Python异步编程原理篇之协程的IO
协程的IO asyncio 作为实现异步编程的库,任务执行中遇到系统IO的时能够自动切换到其他任务.协程使用的IO模型是IO多路复用.在 asyncio 低阶API 一篇中提到过 "以Lin ...
- sublime_text4 2023最新版 激活教程
官网 Sublime HQ - Remarkable Software 东西在教学的时候还是挺好用的,就是要付费购买,穷,没钱 买不起,自己动手丰衣足食. 下载安装包 我现在最新版是4.4152 下面 ...
- java进阶(14)--日期时间处理
一.获取系统当前时间: 1.Date(),精确到毫秒的当前当前时间 2.示例,欧美风格时间格式
- centos7_Lnmp编译安装
17年面试运维岗位的时候,面试官要求输出一份lnmp编译的操作文档,于是有了如下安装nginx+php+mysql,进入正题: 准备环境 环境:centos7.3 软件:nginx-1.12.1 + ...
- 【MicroPython】生成micropython版本头文件 - py\makeversionhdr.py
用法 $ python makeversionhdr.py mpversion.h 实现 带git仓 get_version_info_from_git 使用git指令: git describe ...
- 2023年江苏“领航杯”MISC一个很有意思的题目(别把鸡蛋放在同一个篮子里面)
别把鸡蛋放在同一个篮子里面 题目附件:https://wwzl.lanzoue.com/i6HmX16finnc 1.题目信息 解压压缩包打开附件,获得5141个txt文档,每个文档都有内容,发现是b ...
- [转帖]shell编程:变量的数值计算实践(五)
https://www.cnblogs.com/luoahong/articles/9224495.html 算术运算符 变量的数值(整数)计算 1)(())用法:(此方法很常用)** 范例:sh ...
- [转帖]Linux进程的Uninterruptible sleep(D)状态
https://cloud.tencent.com/developer/article/1581022 Linux系统进程状态 PROCESS STATE CODES Here are the dif ...
- [转帖]kafka指定topic设置消息留存时间
背景 单个主题消息量庞大,需要指定这个主题的消息留存时间缩小点 执行命令 ./bin/kafka-configs.sh --bootstrap-server node1:9092 --entity-t ...