洛谷P2296 寻找道路
\(\Large\textbf{Description:} \large {在有向图 G 中,每条边的长度均为 1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:}\)
\(\large {1.路径上的所有点的出边所指向的点都直接或间接与终点连通。}\)
\(\large {2.在满足条件11的情况下使路径最短。}\)
\(\Large\textbf{Solution:} \large {考虑反向建边,然后从终点开始遍历一遍图,把符合条件的点打上标记,再从终点开始跑一边bfs即可。}\)
\(\Large\textbf{Code:}\)
#include <queue>
#include <cstdio>
#include <algorithm>
#define LL long long
#define gc() getchar()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 2e5 + 5;
int n, m, cnt, s, t, ans, in[10005], head[10005], vis[1005], f[10005];
struct Edge {
int to, next;
}e[N << 1];
struct Node {
int num, dis;
};
queue<Node> q;
inline int read() {
char ch = gc();
int ans = 0;
while (ch > '9' || ch < '0') ch = gc();
while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
return ans;
}
inline void add(int l, int r) {
e[++cnt].to = r;
e[cnt].next = head[l];
head[l] = cnt;
}
inline void dfs(int now) {
f[now] = 1;
for (int i = head[now]; i ; i = e[i].next) {
int u = e[i].to;
++vis[u];
if (f[u]) continue;
dfs(u);
}
}
int main() {
n = read(), m = read();
int l, r;
rep(i, 1, m) l = read(), r = read(), add(r, l), ++in[l];
s = read(), t = read();
dfs(t);
rep(i, 1, n) { f[i] = 0; if (in[i] == vis[i]) f[i] = 1; in[i] = 0; }
q.push((Node){t, 0});
while (!q.empty()) {
Node x = q.front();
q.pop();
for (int i = head[x.num]; i ; i = e[i].next) {
int u = e[i].to;
if (in[u] || !f[u]) continue;
if (u == s) { printf("%d\n", x.dis + 1); return 0; }
else q.push((Node){u, x.dis + 1});
}
}
printf("-1\n");
return 0;
}
\(\large\color{pink}{by\quad Miraclys}\)
洛谷P2296 寻找道路的更多相关文章
- 洛谷P2296 寻找道路==codevs3731 寻找道路
P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
- 洛谷——P2296 寻找道路
P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
- 洛谷P2296 寻找道路 [拓扑排序,最短路]
题目传送门 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
- [NOIP2014] 提高组 洛谷P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- NOIP2014 day2 T2 洛谷P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- 洛谷 [P2296] 寻找道路
反向BFS预处理,求出所有符合题意的点,再正向BFS,(注意对于边权恒为一的点,BFS,比SPFA高效) 输入时n与m分清 #include <iostream> #include < ...
- 洛谷 P2296 寻找道路 —— bfs
题目:https://www.luogu.org/problemnew/show/P2296 第一次用 Emacs 对拍,写了半天: 注意那个 is 赋值的地方很容易错,千万别反复赋值: 一道水题写了 ...
- 洛谷P2296寻找道路
传送门啦 题目中有一个条件是路径上的所有点的出边所指向的点都直接或间接与终点连通. 所以我们要先判断能否走这一个点, $ bfs $ 类似 $ spfa $ 的一个判断,打上标记. 在这我反向建图,最 ...
- 洛谷 P2296 寻找道路【bfs+spfa】
反向建边bfs出不能到t的点,然后对每个能到这些点的点打上del标记,然后spfa的时候不经过这些点即可 #include<iostream> #include<cstdio> ...
- 洛谷P2296 寻找道路_简单BFS
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...
随机推荐
- python多线程下载ts文件
# -*- coding: utf-8 -*- """ Created on Wed Aug 22 15:56:19 2018 @author: Administrato ...
- RedHat OpenShift QuickStart 1.2
一.在容器中传入/出文件 1. 创建一个初始化项目 oc login -u developer -p developer oc new-project myproject 2. 在容器中下载文件 先通 ...
- electron 查看版本信息
console.log(process) console.log(process.versions.electron) process 里包含很多信息:
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- .net工作流设计器
源码地址 Github: https://github.com/chengderen/Smartflow-Sharp 简要说明 https://www.smartflow-sharp.com/doc. ...
- Dam-list
1. Dam 2. 溃坝 3. 水坝对环境的影响 4. 水坝列表 4.1 黄河干流水电站列表 4.2 长江干流水电站列表 4.3 长江水系支流 431. 大渡河 432. 乌江 433. 雅砻江 43 ...
- redhat 7.6 密码破解(无光盘)
开机,在下面界面按e 找到linux16 在最尾输入 rd.break 按 Ctrl+x 输入 mount -o remount,rw /sysroot 输入chroot /sysroot sh ...
- target信息异常
当工程的编译target信息异常的时候,可以删除YourProjectName.xcodedeprij/xcuserdate目录. 该目录存有当前用户的各种工程状态信息,删除后重启Xcode,Xcod ...
- TensorFlow Serving简介
一.TensorFlow Serving简介 TensorFlow Serving是GOOGLE开源的一个服务系统,适用于部署机器学习模型,灵活.性能高.可用于生产环境. TensorFlow Ser ...
- Codeforces 598D:Igor In the Museum
D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...