题意

有一个 \(N\) 个点, \(M\) 条边的有向图, 初始有一个机器人在 \(1\) 号点. 每个时刻, 这个机器人会随机选择一条从该点出发地边并通过.当机器人到达点 \(N\) 时, 它就会自动关闭.

然而这个机器人如果在某个时刻到达自己曾经到过的点的话, 它就会爆炸. 因此, 你决定对机器人实施一些命令, 让它在某些时候按照规定的边走, 而非随机选择.

问对机器人最少使用多少条命令可以让它安全到达点 \(N\) .

\(N, M \le 10^6\)

题解

十分巧妙的一道好题~

首先可以无视掉 “不能到达曾经到过的点” 的限制, 因为最优答案一定不会存在这种情况.

因为到达曾经到过的点,你至少要付出更多代价才能回到这个点,所以绝对不优。

然后我们就可以考虑一个 \(dp\) 了,令 \(dp_u\) 为 \(u\) 走到 \(T\) 需要的最少命令。

那么显然有一个转移:

\[dp_u=\min_{(u, v)} \{\min\{dp_v\}+1,\max\{dp_v\}\}
\]

这个意义是很明显的,就不解释了。

这个本质上是个 \(0 / 1\) BFS 问题,用个双端队列维护就行了,\(0\) 加到队首, \(1\) 加到队尾就行了。

具体实现的时候,我们只有在第一次到达这个点的时候会更新 \(\min\{dp_v\}+1\) ,因为是 BFS 最早到的肯定是距离较小的点。

也就是队列中的点 \(dis\) 单调不下降。

然后最后一次到达这个点才会更新 \(\max\{dp_v\}\) ,同样这是 BFS 最晚到的点。

每个点我们只会访问一次,所以最后一次到达就是它入度减少到 \(0\) 的时候。

复杂度是 \(O(n + m)\) 的。

总结

对于一类图上有关 \(dp\) 的 \(\min,\max\) 问题能考虑 BFS 队列的 \(dis\) 单调不下降的性质来转移。

代码

记得要把边反向,以及入度也要反向。


#include <bits/stdc++.h> #define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__) using namespace std; inline bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
inline bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;} inline int read() {
int x = 0, fh = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * fh;
} void File() {
#ifdef zjp_shadow
freopen ("D.in", "r", stdin);
freopen ("D.out", "w", stdout);
#endif
} const int N = 1e6 + 1e3; int n, m, deg[N], dp[N]; vector<int> G[N]; int S, T; bitset<N> vis;
void Bfs() {
Set(dp, -1); deque<int> Q; Q.push_front(T); dp[T] = 0;
while (!Q.empty()) {
int u = Q.front(); Q.pop_front();
if (u == S) return ;
if (vis[u]) continue ; vis[u] = true;
for (int v : G[u]) if (!-- deg[v]) {
if (!~dp[v] || dp[u] < dp[v]) dp[v] = dp[u], Q.push_front(v);
} else if (!~dp[v]) dp[v] = dp[u] + 1, Q.push_back(v);
}
} int main () { File(); n = read(); m = read();
For (i, 1, m) {
int u = read(), v = read();
G[v].push_back(u); ++ deg[u];
} S = read(), T = read(); Bfs(); printf ("%d\n", dp[S]); return 0;
}

Codeforces 346D Robot Control(01BFS)的更多相关文章

  1. Codeforces 346D Robot Control DP spfa 01BFS

    题意及思路:https://www.cnblogs.com/zjp-shadow/p/9562888.html 这题由于性质特殊,可以用01BFS来进行DP的转移. 代码: #include < ...

  2. Codeforces346D. Robot Control

    D. Robot Control time limit per test 6 seconds memory limit per test 256 megabytes input standard in ...

  3. LTE Module User Documentation(翻译14)——Uplink Power Control(上行功率控制)

    LTE用户文档 (如有不当的地方,欢迎指正!) 20 Uplink Power Control(上行功率控制)   上行功率控制功能默认是开启的.用户可以通过设置布尔属性 ns3::LteUePhy: ...

  4. [Notes] Reading Notes on [Adaptive Robot Control – mxautomation J. Braumann 2015]

    Reading sources: 1.Johannes Braumann, Sigrid Brell-Cokcan, Adaptive Robot Control (ARC  ) Note: buil ...

  5. 与众不同 windows phone (2) - Control(控件)

    原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...

  6. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  7. HDU 4289 Control (网络流,最大流)

    HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...

  8. Codeforces I. Producing Snow(优先队列)

    题目描述: C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. PHP实用代码片段(一)

    1. 发送 SMS 在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息.下面的 PHP 代码就实现了发送 SMS 的功能. 为了使用任何的语言 ...

  2. hibernate异常找不到get方法org.hibernate.PropertyNotFoundException: Could not find a getter for did in class com.javakc.hibernate.manytomany.entity.CourseEntity

    属性的get方法没找到,可能是CourseEntity类中对应属性没有get方法,如果有就看CourseEntity.hbm.xml属性名称,应该是写错了不和CourseEntity类中属性名相同,修 ...

  3. 阿里云服务器使用镜像市场上的环境以后sql不能远程问题

    关于阿里云的服务器,首先要说的就是买了以后是没有环境的,什么都需要自己配置,也是在这个上面栽了很多跟头最后去的镜像市场买的一个IIS8+SQL2016的asp.net环境 怎么说呢,感觉有些问题的本源 ...

  4. openstack-KVM安装与使用

    一.KVM安装 1.安装条件 VT-x BIOS Intel9R) Virtualization Tech [Enabled] cat /proc/cpuinfo | grep -e vmx -e n ...

  5. Jmeter操作之跨线程组传递参数

    思路:将某一线程组内的变量通过“__setProperty”函数设置成jmeter的全局变量,在另一线程组中通过“__P”函数调用即可. 1.添加-后置处理器-BeanShell PostProces ...

  6. 使用PHPExcel导出数据库表结构及内容

    导出表结构: mysql> desc user ; +----------+--------------+------+-----+---------------------+--------- ...

  7. 【转帖】介绍 .NET Standard

    [译]介绍 .NET Standard https://zhuanlan.zhihu.com/p/24267356 跟开发争执过 自己不会写代码 的确不好. 若有任何对翻译的建议,烦请指正 有任何问题 ...

  8. spring AOP源码分析(三)

    在上一篇文章 spring AOP源码分析(二)中,我们已经知道如何生成一个代理对象了,那么当代理对象调用代理方法时,增强行为也就是拦截器是如何发挥作用的呢?接下来我们将介绍JDK动态代理和cglib ...

  9. sqlserver常用语法

    --临时表 IF OBJECT_ID('tempdb..#Entry') is not null BEGIN   DROP TABLE #Entry   END ------------------- ...

  10. C# Note17: 使用Ionic.Zip.dll实现解压缩文件

    首先下载ionic.Zip.dll,然后在项目中添加该引用,之后就可以在cs中使用了: using Ionic.Zip; #region Ionic.Zip压缩文件 private readonly ...