Codeforces 346D Robot Control DP spfa 01BFS
题意及思路:https://www.cnblogs.com/zjp-shadow/p/9562888.html
这题由于性质特殊,可以用01BFS来进行DP的转移。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
vector<int> G[maxn];
deque<int> q;
int dp[maxn], deg[maxn];
bool v[maxn];
void add(int x, int y) {
G[x].push_back(y);
deg[y]++;
}
void bfs(int s, int t) {
memset(dp, -1, sizeof(dp));
dp[s] = 0;
q.push_back(s);
while(q.size()) {
int x = q.front();
q.pop_front();
if(v[x]) continue;
v[x] = 1;
//if(x == t) return;
for (auto y : G[x]) {
if(!--deg[y]) {
if(dp[y] > dp[x] || dp[y] == -1) {
dp[y] = dp[x];
q.push_front(y);
}
} else if(dp[y] == -1) {
dp[y] = dp[x] + 1;
q.push_back(y);
}
}
}
}
int main() {
int n, m, x, y, s, t;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
add(y, x);
}
scanf("%d%d", &s, &t);
bfs(t, s);
printf("%d\n", dp[s]);
}
但是实际上,遇到有后效性的DP方程时,如果是一个DAG,一般用spfa来进行DP的状态转移,因为spfa是迭代的思想,如果所有状态都收敛了(不能更新了),就完成了转移。
思路来源:https://www.cnblogs.com/huibixiaoxing/p/7715898.html
代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000010;
vector<int> G[maxn], G1[maxn];
int dp[maxn];
bool v[maxn];
void add(int x, int y) {
G[x].push_back(y);
G1[y].push_back(x);
}
queue<int> q;
void spfa(int s, int t) {
memset(dp, 0x3f, sizeof(dp));
dp[s] = 0;
v[s] = 1;
q.push(s);
while(q.size()) {
int x = q.front();
q.pop();
v[x] = 0;
for (auto y : G[x]) {
if(dp[y] > dp[x] + 1) {
dp[y] = dp[x] + 1;
if(!v[y]) {
q.push(y);
v[y] = 1;
}
}
}
int tmp = 0;
for (auto y : G1[x]) {
tmp = max(tmp, dp[y]);
}
if(dp[x] > tmp) {
dp[x] = tmp;
if(!v[x]) {
q.push(x);
v[x] = 1;
}
}
}
}
int main() {
int n, m, x, y, s, t;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
add(y, x);
}
scanf("%d%d", &s, &t);
spfa(t, s);
if(dp[s] == INF) dp[s] = -1;
printf("%d\n", dp[s]);
}
Codeforces 346D Robot Control DP spfa 01BFS的更多相关文章
- Codeforces 346D Robot Control(01BFS)
题意 有一个 \(N\) 个点, \(M\) 条边的有向图, 初始有一个机器人在 \(1\) 号点. 每个时刻, 这个机器人会随机选择一条从该点出发地边并通过.当机器人到达点 \(N\) 时, 它就会 ...
- Codeforces346D. Robot Control
D. Robot Control time limit per test 6 seconds memory limit per test 256 megabytes input standard in ...
- [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 ...
- POJ 3182 The Grove [DP(spfa) 射线法]
题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...
- 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)
这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...
- BZOJ1003物流運輸 DP + SPFA
@[DP, SPFA] Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要\(n\)天才能运完.货物运输过程中一般要转 停好几个码头.物流公司通常会设计一条固定的运 ...
- HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j] ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)
pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...
随机推荐
- spring 事物(一)—— 事物详解
事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 事务的特性 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么 ...
- 三、IDS4建立authorization server
建立authorization server 一.环境搭建 1.创建项目 2.引用NuGet的identityserver4 3.配置asp.net core 管道 打开Startup.cs, 编辑C ...
- 利用Stream模式进行文件拷贝
const fs = require('fs'); const file = fs.createReadStream("readfile.js"); const outputFil ...
- html使用字符串拼接js函数时传字符串参数
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- youlexuan父类配置
<!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version> ...
- c# 泛型的抗变和协变
namespace test { // 泛型的协变,T 只能作为返回的参数 public interface Class1<out T> { T Get(); int Count { ge ...
- ruby puts语法
str = "Welcom to china" str1 = str puts str + " 1" puts str1 + " 1" de ...
- PHP filter_input_array() 函数
定义和用法 filter_input_array() 函数从脚本外部获取多项输入(比如表单输入),并进行过滤. 该函数对过滤多个输入变量很有用,无需重复调用 filter_input(). 该函数可从 ...
- apue 第6章 系统数据文件和信息
在给出用户登录名或数值用户ID后,这两个函数就能查看相关项. #include <sys/types.h> #include <pwd.h> struct passwd *ge ...
- python 100day notes (1)
x1 + x2 +x3 + x4 = 8 多少正整数解 上面的问题等同于将8个苹果分成四组每组至少一个苹果有多少种方案 即用三个隔板插7个空位. 答案C(7,3) =35 # __name__是Pyt ...