D. Robot Control
time limit per test

6 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The boss of the Company of Robot is a cruel man. His motto is "Move forward Or Die!". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called "Three Laws of Robotics":

  • Law 1. The Robot will destroy itself when it visits a vertex of the graph which it has already visited.
  • Law 2. The Robot will destroy itself when it has no way to go (that is when it reaches a vertex whose out-degree is zero).
  • Law 3. The Robot will move randomly when it has multiple ways to move (that is when it reach a vertex whose out-degree is more than one). Of course, the robot can move only along the directed edges of the graph.

Can you imagine a robot behaving like that? That's why they are sold at a very low price, just for those who are short of money, including mzry1992, of course. mzry1992 has such a robot, and she wants to move it from vertex s to vertex t in a directed graph safely without self-destruction. Luckily, she can send her robot special orders at each vertex. A special order shows the robot which way to move, if it has multiple ways to move (to prevent random moving of the robot according to Law 3). When the robot reaches vertex t, mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from s to t, she can always find a way to reach the goal (whatever the vertex t has the outdegree of zero or not).

Sample 2

However, sending orders is expensive, so your task is to find the minimum number of orders mzry1992 needs to send in the worst case. Please note that mzry1992 can give orders to the robot while it is walking on the graph. Look at the first sample to clarify that part of the problem.

Input

The first line contains two integers n (1 ≤ n ≤ 106) — the number of vertices of the graph, and m (1 ≤ m ≤ 106) — the number of edges. Then m lines follow, each with two integers ui and vi (1 ≤ ui, vi ≤ n; vi ≠ ui), these integers denote that there is a directed edge from vertex ui to vertex vi. The last line contains two integers s and t (1 ≤ s, t ≤ n).

It is guaranteed that there are no multiple edges and self-loops.

Output

If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1.

Examples
Input
4 6
1 2
2 1
1 3
3 1
2 4
3 4
1 4
Output
1
Input
4 5
1 2
2 1
1 3
2 4
3 4
1 4
Output
1
Note

Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can choose the "bad" outgoing edge (return to vertex 1) according Law 3. So, the answer is one.

【题解】

dp[u]表示从u这个点到终点需要的最小代价

dp[u] = min(max(dp[v]), min(dp[u]) + 1), dp[t] = 1, u - > v

可以用SPFA转移

对于点u,用u去松弛u的入边的min(dp[u]) + 1,用u的出边的点去松弛u的max(dp[v])

时间复杂度O(玄学)

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b)) inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
} inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = + ;
const int MAXM = + ; struct Edge
{
int u,v,nxt;
Edge(int _u, int _v, int _nxt){u = _u;v = _v;nxt = _nxt;}
Edge(){}
}edge1[MAXM], edge2[MAXN];
int head1[MAXN], head2[MAXN], cnt1, cnt2;
inline void insert(int a, int b)
{
edge1[++cnt1] = Edge(a,b,head1[a]);
head1[a] = cnt1;
edge2[++cnt2] = Edge(b,a,head2[b]);
head2[b] = cnt2;
} int n,m,s,t,dp[MAXN],b[MAXN];
std::queue<int> q; /*
dp[u] = min(min(dp[v]) + 1, max(dp[v]))
*/ void SPFA()
{
b[t] = ;memset(dp, 0x3f, sizeof(dp));dp[t] = ;q.push(t);
while(q.size())
{
int u = q.front();q.pop();b[u] = ;
for(register int pos = head2[u];pos;pos = edge2[pos].nxt)
{
int v = edge2[pos].v;
if(dp[u] + < dp[v])
{
dp[v] = dp[u] + ;
if(!b[v])
{
b[v] = ;
q.push(v);
}
}
}
int tmp = ;
for(register int pos = head1[u];pos;pos = edge1[pos].nxt) tmp = max(tmp, dp[edge1[pos].v]);
if(tmp < dp[u])
{
dp[u] = tmp;
if(!b[u])
{
b[u] = ;
q.push(u);
}
}
}
} int main()
{
read(n), read(m);
for(register int i = ;i <= m;++ i)
{
int tmp1,tmp2;
read(tmp1), read(tmp2);
insert(tmp1, tmp2);
}
read(s), read(t);
SPFA();
if(dp[s] == INF)dp[s] = -;
printf("%d\n", dp[s]);
return ;
}

Codeforces346D

Codeforces346D. Robot Control的更多相关文章

  1. [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 ...

  2. Codeforces 346D Robot Control(01BFS)

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

  3. Codeforces 346D Robot Control DP spfa 01BFS

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

  4. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  5. NBU expired Media,Media ID not found in EMM database

    Subject:When attempting to expire a media in Veritas NetBackup (tm) 6.0 with the bpexpdate command, ...

  6. SLAM学习笔记(3)相关概念

    SIFT,即尺度不变特征变换(Scale-invariant feature transform,SIFT),是用于图像处理领域的一种描述子.这种描述具有尺度不变性,可在图像中检测出关键点,是一种局部 ...

  7. HOWTO: Create native-looking iPhone/iPad applications from HTML, CSS and JavaScript

    HOWTO: Create native-looking iPhone/iPad applications from HTML, CSS and JavaScript Though it's not ...

  8. ROS常用三維機器人仿真工具Gazebo教程匯總

    參考網址: 1. http://gazebosim.org/tutorials 2. http://gazebosim.org/tutorials/browse Gazebo Tutorials Ga ...

  9. ROS_Kinetic_x 目前已更新的常用機器人資料 rosbridge agvs pioneer_teleop nao TurtleBot

    Running Rosbridge Description: This tutorial shows you how to launch a rosbridge server and talk to ...

随机推荐

  1. CSS3新增(选择器{属性选择器,结构伪类选择器,伪元素选择器})

    1.属性选择器 属性选择器,可以根据元素特定的属性来选择元素,这样就不用借助 类 或者 id选择器. E [ att ]   选择具有 att 属性的 E 元素   例如:input [ value ...

  2. 前端学习(十)初识js(笔记)

    js事件(公有属性) onclick=""  当点击...时! onmouseover="" 当鼠标移入...时!onmouseout="" ...

  3. PHP创建多级目录文件夹

    PHP创建多级目录的代码实例如下: <?php function create_dir($dirName) { // 去除输入目录名中的空格部分 $dirName = trim($dirName ...

  4. 1.隐藏继承的成员new / 虚方法(override)/ abstract / 多态 ----- 重写

    总结: 1. 在继承上, new/override没区别 2. 在多态上,new不支持多态,override支持 在C#中改变类中相同名称的方法的实现过程中有三种方式:重载.重写和覆盖. 重载:指具有 ...

  5. 利用Python批量重命名一系列文件名杂乱的文件

    假设目录下面有这样一系列命令杂乱的文件: OPENFOAM -TRAINING- PART- #1.pdf OPENFOAM - TRAINING- PART- #2.pdf OPENFOAM- TR ...

  6. Python文件路径操作

    print(os.environ.get('HOME')) # 打印`HOME`这个环境变量 /Users/<> file_path = os.environ.get('HOME') + ...

  7. java基础之toString的使用方法

    toString方法,其返回值类型为String类型,用于返回表示对象值的字符串,返回类名和它的引用地址(散列码 hashCode)toString方法是Object类中的一个实例方法,所有的java ...

  8. Dubbo 如何成为连接异构微服务体系的最佳服务开发框架

    从编程开发的角度来说,Apache Dubbo (以下简称 Dubbo)首先是一款 RPC 服务框架,它最大的优势在于提供了面向接口代理的服务编程模型,对开发者屏蔽了底层的远程通信细节.同时 Dubb ...

  9. Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class

    ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶 ...

  10. svn启动服务

    bin目录添加到环境变量classpathsvn --version 查看版本svnadmin create D:\\xx 创建本地中央仓库启动svn服务 cmd命令 svnserve -d -r D ...