传送门啦

题目中有一个条件是路径上的所有点的出边所指向的点都直接或间接与终点连通。

所以我们要先判断能否走这一个点, $ bfs $ 类似 $ spfa $ 的一个判断,打上标记。

在这我反向建图,最后跑最短路的时候就从终点跑到起点,也是一样的。在最短路中加上一个判断就好了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define re register
using namespace std;
const int maxn = 10005;
const int maxm = 200005; inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0' ) { if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3 ) + ch - '0';ch = getchar();}
return x * f;
} int n , m , x , y , s , t ;
int head[maxn] , tot ;
bool vis[maxm] , ok[maxm] ; struct Edge{
int from,to,next,val;
}edge[maxm << 1]; struct Node {
int u , d;
bool operator < (const Node &f) const {
return d > f.d;
}
}; inline void add(int u , int v , int w){
edge[++tot].from = u ;
edge[tot].to = v ;
edge[tot].val = w;
edge[tot].next = head[u];
head[u] = tot ;
} inline void bfs(int t){
queue<int> que;
memset(vis , false , sizeof(vis) );
que.push(t);
vis[t] = true;
ok[t] = true;
while( !que.empty() ){
int cur = que.front();
que.pop();
for(re int i = head[cur] ; i ; i = edge[i].next){
int v = edge[i].to ;
if(vis[v] == false){
ok[v] = true ;
vis[v] = true;
que.push(v);
}
}
}
} int dis[maxn];
inline void dijk(int t){
for(re int i = 1 ;i <= n ; ++i) dis[i] = 1e9;
priority_queue<Node> q;
dis[t] = 0;
q.push( (Node) {t , dis[t]});
while( ! q.empty() ){
Node cur = q.top();
q.pop() ;
int d = cur.d , u = cur.u ;
if(d != dis[u]) continue;
for(re int i = head[u] ; i; i = edge[i].next){
int v = edge[i].to;
if(ok[v]) continue;
if(dis[v] > dis[u] + edge[i].val){
dis[v] = dis[u] + edge[i].val;
q.push( (Node) {v , dis[v]} );
}
}
}
} int main(){
n = read(); m = read();
for(re int i = 1 ; i <= m ; ++i) {
x = read(); y = read();
add(y , x , 1);
}
s = read() ; t = read() ;
bfs(t) ;
for(re int i = 1 ; i <= n ; ++i){
vis[i] = ok[i];
}
memset(ok , false , sizeof(ok));
for(int i = 1 ; i <= n ; ++i){
if(!vis[i]) {
for(re int j = head[i] ; j ; j = edge[j].next) {
int v = edge[j].to;
ok[v] = true;
}
}
}
dijk(t) ;
if(dis[s] != 1e9) printf("%d\n",dis[s]);
else printf("-1\n");
return 0;
}

洛谷P2296寻找道路的更多相关文章

  1. 洛谷P2296 寻找道路==codevs3731 寻找道路

    P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...

  2. 洛谷——P2296 寻找道路

    P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...

  3. 洛谷P2296 寻找道路 [拓扑排序,最短路]

    题目传送门 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...

  4. [NOIP2014] 提高组 洛谷P2296 寻找道路

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  5. NOIP2014 day2 T2 洛谷P2296 寻找道路

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  6. 洛谷 [P2296] 寻找道路

    反向BFS预处理,求出所有符合题意的点,再正向BFS,(注意对于边权恒为一的点,BFS,比SPFA高效) 输入时n与m分清 #include <iostream> #include < ...

  7. 洛谷 P2296 寻找道路 —— bfs

    题目:https://www.luogu.org/problemnew/show/P2296 第一次用 Emacs 对拍,写了半天: 注意那个 is 赋值的地方很容易错,千万别反复赋值: 一道水题写了 ...

  8. 洛谷 P2296 寻找道路【bfs+spfa】

    反向建边bfs出不能到t的点,然后对每个能到这些点的点打上del标记,然后spfa的时候不经过这些点即可 #include<iostream> #include<cstdio> ...

  9. 洛谷P2296 寻找道路_简单BFS

    Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...

随机推荐

  1. logger.debug的用处

    原文:https://www.cnblogs.com/xiangkejin/p/6426761.html logger.debug的用处 简单的说,就是配合log的等级过滤输出 根据你log4j的配置 ...

  2. 利用solr实现商品的搜索功能

      后期补充: 为什么要用solr服务,为什么要用luncence? 问题提出:当我们访问购物网站的时候,我们可以根据我们随意所想的内容输入关键字就可以查询出相关的内容,这是怎么做到呢?这些随意的数据 ...

  3. shiro权限认证与授权

    什么是shiro? Shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 为什么要用sh ...

  4. KVM基本实现原理

    KVM 虚拟化技术概述 http://blog.csdn.net/yearn520/article/details/6461047 KVM 虚拟化技术在 AMD 平台上的实现 1.http://www ...

  5. 2017 清北济南考前刷题Day 2 afternoon

    期望得分:100+60+70=230 实际得分:0+60+0=60 T1 可以证明如果一对括号原本就匹配,那么这对括号在最优解中一定不会被分开 所以用栈记录下没有匹配的括号 最后栈中一定是 一堆右括号 ...

  6. [SDOI2016 Round1] 数字配对

    COGS 2221. [SDOI2016 Round1] 数字配对 http://www.cogs.pro/cogs/problem/problem.php?pid=2221 ★★★   输入文件:m ...

  7. spring-boot Test for Controller

    spring-boot  controller 测试示例: 单元测试类 package com.zzhi; import com.fasterxml.jackson.databind.ObjectMa ...

  8. 超酷JQuery动画分页按钮,鼠标悬停滑动展开

    1.效果及功能说明 animate动画分页按钮制作鼠标悬停分页按钮上滑动展开分页按钮,鼠标离开后分页按钮收缩 2.实现原理 主要是靠动画方法,来让原本的箭头图像的长度发生变长,正好可以融入下标题的文字 ...

  9. 在windows的IDEA运行Presto

    After building Presto for the first time, you can load the project into your IDE and run the server. ...

  10. 【BZOJ】2655: calc 动态规划+拉格朗日插值

    [题意]一个序列$a_1,...,a_n$合法当且仅当它们都是[1,A]中的数字且互不相同,一个序列的价值定义为数字的乘积,求所有序列的价值和.n<=500,A<=10^9,n+1< ...