Problem

P2296 【寻找道路】

solution

首先声明,这题我用了spfa,而:

关于spfa:它死了。

杀手: NOI 2018−T1 出题人

感谢出题人,没有卡spfa

  • 用时: 20ms
  • 空间: 5082KB(4.74MB)
  • 代码长度: 3.32KB
  • 提交记录: R9776986

先说思路:

  1. 首先,要处理出哪些点不能直接或间接与终点连通

    • 函数:void live(void)
    • 这里的方法是建反图跑spfa
    • 不能直接或间接与终点连通的点存在temp_alive数组里,1为活着,0为死了
  2. 其次,要把所有指向不能直接或间接与终点连通的那些点的那些点设置为死了
    • 在函数live()里执行
    • 存到alive数组,1为活着,0为死了
    • 注意:第二步的结果不能直接直接存储在第一步的数组里,否则会杀掉一些有用的点
  3. 一遍spfa求最短路,求的过程中排除所有那些死了的点。
    • 函数:void spfa(void)
    • 注意路径长度均为1
  4. 完结散花♪(^∇^*)

Code

// luogu-judger-enable-o2
/*
Problem: P2296 【寻找道路】
Author: 航空信奥
Date: 2018/08/16
*/
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#define Clear(a, x) memset(a, x, sizeof(a))
using namespace std; namespace hkxa { /* 防重名 */
inline char Getchar();
template <typename _TpInt> inline _TpInt read();
template <typename _TpInt> inline void write(_TpInt x); # define Max_N vector<int> to[Max_N];
vector<int> fr[Max_N]; /* 反图 */
int n, m;
int start, finish;
bool alive[Max_N] = {};
int dis[Max_N] = {}; void live()
{
bool temp_alive[Max_N] = {};
queue <int> q;
q.push(finish);
temp_alive[finish] = ;
int point;
while (!q.empty()) {
point = q.front();
q.pop();
for (int i = ; i < fr[point].size(); i++) {
if (!temp_alive[fr[point][i]]) {
q.push(fr[point][i]);
temp_alive[fr[point][i]] = ;
}
}
}
Clear(alive, );
for (int i = ; i <= n; i++) {
if (!temp_alive[i]) {
alive[i] = ;
for (int j = ; j < fr[i].size(); j++) {
alive[fr[i][j]] = ;
}
}
}
} void spfa()
{
Clear(dis, 0x3f);
dis[start] = ;
queue <int> q;
q.push(start);
int point;
while (!q.empty()) {
point = q.front();
q.pop();
for (int i = ; i < to[point].size(); i++) {
if (alive[to[point][i]] && dis[point] + < dis[to[point][i]]) {
q.push(to[point][i]);
dis[to[point][i]] = dis[point] + ;
}
}
}
} int main()
{
n = read<int>();
m = read<int>();
int f, t;
for (int i = ; i < m; i++) {
f = read<int>();
t = read<int>();
to[f].push_back(t);
fr[t].push_back(f);
}
start = read<int>();
finish = read<int>(); live();
spfa();
if (dis[finish] == 0x3f3f3f3f)
dis[finish] = -;
write(dis[finish]);
puts(""); return ;
} char BufferRead[ << ];
int rLen = , rPos = ;
inline char Getchar()
{
if (rPos == rLen) rPos = , rLen = fread(BufferRead, , << , stdin);
if (rPos == rLen) return EOF;
return BufferRead[rPos++];
} template <typename _TpInt>
inline _TpInt read()
{
register int flag = ;
register char c = Getchar();
while ((c > '' || c < '') && c != '-')
c = Getchar();
if (c == '-') flag = -, c = Getchar();
register _TpInt init = (c & );
while ((c = Getchar()) <= '' && c >= '')
init = (init << ) + (init << ) + (c & );
return init * flag;
} template <typename _TpInt>
inline void write(_TpInt x)
{
if (x < ) {
putchar('-');
write<_TpInt>(~x + );
}
else {
if (x > ) write<_TpInt>(x / );
putchar(x % + '');
}
}
} int main()
{
hkxa::main();
return ;
}

洛谷 题解 P2296 【寻找道路】的更多相关文章

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

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

  2. 洛谷——P2296 寻找道路

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

  3. 洛谷P1462-通往奥格瑞玛的道路-二分+最短路

    洛谷P1462-通往奥格瑞玛的道路 题目描述 在艾泽拉斯,有\(n\)个城市.编号为\(1,2,3,...,n\). 城市之间有\(m\)条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联 ...

  4. DP【洛谷P1704】 寻找最优美做题曲线

    [洛谷P1704] 寻找最优美做题曲线 题目背景 nodgd是一个喜欢写程序的同学,前不久(好像还是有点久了)洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情, ...

  5. 洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)

    洛谷P1462 通往奥格瑞玛的道路 二分费用. 用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) .二分时需要不断记录合法的 \(mid\) 值. 这里建 ...

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

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

  7. 洛谷 题解 UVA572 【油田 Oil Deposits】

    这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...

  8. 洛谷 题解 P1600 【天天爱跑步】 (NOIP2016)

    必须得说,这是一道难题(尤其对于我这样普及组205分的蒟蒻) 提交结果(NOIP2016 天天爱跑步): OJ名 编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间 Libre ...

  9. 洛谷P4319 变化的道路

    题意:给定图,每条边都有一段存在时间.求每段时间的最小生成树. 解:动态MST什么毒瘤...洛谷上还是蓝题... 线段树分治 + lct维护最小生成树. 对时间开线段树,每条边的存在时间在上面会对应到 ...

随机推荐

  1. Havok Physics 2012(1)

    目录 Chapter 1. Introduction 1. What is a Physics Engine? Chapter 1. Introduction ​ 欢迎来到Havok Physics ...

  2. 误删tree命令如何恢复

    误删tree命令如何恢复 考察rpm,yum的用法 一.删除tree命令,tree命令不可用 [root@centos7 ~]# which tree /usr/bin/tree [root@cent ...

  3. Material for oauth 2

    oauth 2 in 8 steps:  https://knpuniversity.com/screencast/oauth Live demo of oauth 2 (with server im ...

  4. hdu 3342 Legal or Not (topsort)

    Legal or NotTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. php相关知识(一)

    php是服务器端脚本语言.可以生成动态页面内容,可以对数据库中的数据库进行编辑. php变量以$符号开始,后面是变量名,变量名以字母或下划线开始,变量名不能包含空格,变量名区分大小写. php的数据类 ...

  6. IDEA+SpringBoot+Mybatis+maven分布式项目框架的搭建

    参考文章:https://blog.csdn.net/qq_34410726/article/details/98214992 一.maven分布式工程的基本架构 demo  #父工程模块,主要用来定 ...

  7. 使用CGLIB实现动态代理

    参考:https://blog.csdn.net/yhl_jxy/article/details/80633194#comments CGLIB动态代理 定义:CGLIB(code genaratio ...

  8. [Part 3] 在Ubuntu 16.04源码编译PCL 1.8.1支持VTK和QT

    本文首发于个人博客https://kezunlin.me/post/137aa5fc/,欢迎阅读! Part-3: Install and Configure PCL 1.8.1 with vtk q ...

  9. 小白学 Python 爬虫(4):前置准备(三)Docker基础入门

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  10. 20191107-3 beta week 2/2 Scrum立会报告+燃尽图 02

    此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/9955] 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 ...