洛谷P2296寻找道路
传送门啦
题目中有一个条件是路径上的所有点的出边所指向的点都直接或间接与终点连通。
所以我们要先判断能否走这一个点, $ 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寻找道路的更多相关文章
- 洛谷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】
反向建边bfs出不能到t的点,然后对每个能到这些点的点打上del标记,然后spfa的时候不经过这些点即可 #include<iostream> #include<cstdio> ...
- 洛谷P2296 寻找道路_简单BFS
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...
随机推荐
- 单点登录(八)-----遇到问题-----Application Not Authorized to Use CAS
配置好cas后访问cas client 并没有跳转到登录页面,而是页面报错误提示: Application Not Authorized to Use CAS. The application yo ...
- 团体程序设计天梯赛 L1-049. 天梯赛座位分配(测试数据+不同方法)
Data: /*33 2 1#11 4 7 10 13 16 19 22 25 2831 33 35 37 39 41 43 45 47 4951 53 55 57 59 61 63 65 67 69 ...
- Qt 状态栏设置
版权声明 该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处! 导语 在程序主窗口QMainWindow中,主要包含菜单栏,工具栏,中心部件和状 ...
- Laravel 限流中间件 throttle 简析
1. 在Laravel 中配置 在 app\Http\Kernel.php 中,默认添加到中间件组 api 下,1分钟60次. 2. 限流原理 获取唯一请求来源,进行唯一标识(key) 获取该请求请求 ...
- Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)
Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...
- Linux_用户管理&权限管理
2017年1月11日, 星期三 Linux_用户管理&权限管理 1. Linux用户管理&权限管理 终端的概念: tty 查看登录的终端 类型 user group oth ...
- python日记---day1
Life is short,Test in python 一.输入输出 1.用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world' print('h ...
- 【js学习笔记】去除省、市、区、特别行政区、自治区
不是很懂js,以前去除这些省.市.区的时候都是用的分支判断indexOf,如果!=-1则replace一次,今天看同事的代码,发现还有更简单的办法... var areaName = str.repl ...
- spring框架学习(八)spring管理事务方式之注解配置
1.DAO AccountDao.java package cn.mf.dao; public interface AccountDao { //加钱 void increaseMoney(Integ ...
- react-music React全家桶项目,精品之作!
React-Music 全家桶项目,精品之作! 一.简介 该项目是基于React全家桶开发的一个音乐播放器,技术栈采用:Webpack + React + React-redux + React-ro ...