洛谷 P2296 寻找道路
来源:互联网 发布:apache maven怎么安装 编辑:程序博客网 时间:2024/06/06 14:16
题目描述
在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:
1 .路径上的所有点的出边所指向的点都直接或间接与终点连通。
2 .在满足条件1 的情况下使路径最短。
注意:图G 中可能存在重边和自环,题目保证终点没有出边。
请你输出符合条件的路径的长度。
【题目分析】
两次bfs
【代码】
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n,m,s,t;int h[400001],to[400001],ne[400001],en=0;int can[400001];int dis[100001];int vis[100001];void add(int a,int b){ to[en]=b; ne[en]=h[a]; h[a]=en++;}void BFS1(){ memset(vis,0,sizeof vis); queue <int> q; while (!q.empty()) q.pop(); q.push(t);can[t]=1; while (!q.empty()) { int x=q.front();q.pop(); for (int i=h[x];i>=0;i=ne[i]) { if (!(i&1)) continue; can[to[i]]=1; if (!vis[to[i]]) { q.push(to[i]); vis[to[i]]=1; } } } for (int i=1;i<=n;++i) if (!can[i]) q.push(i); while (!q.empty()) { int x=q.front();q.pop(); for (int i=h[x];i>=0;i=ne[i]) { if (!(i&1)) continue; can[to[i]]=0; } }// for (int i=1;i<=n;++i) printf("%d ",can[i]); printf("\n");}void BFS2(){ memset(vis,0,sizeof vis); memset(dis,0x3f,sizeof dis); queue <int> q; while (!q.empty()) q.pop(); q.push(s); dis[s]=0; while (!q.empty()) { int x=q.front();q.pop(); for (int i=h[x];i>=0;i=ne[i]) if (!(i&1)&&can[to[i]]) { dis[to[i]]=min(dis[to[i]],dis[x]+1); if (!vis[to[i]]) { q.push(to[i]); vis[to[i]]=1; } } } printf("%d\n",dis[t]);}int main(){ memset(h,-1,sizeof h); scanf("%d%d",&n,&m); for (int i=1;i<=m;++i) { int a,b; scanf("%d%d",&a,&b); add(a,b); add(b,a); } scanf("%d%d",&s,&t); BFS1(); BFS2();}
0 0
- 【洛谷P2296】寻找道路
- 洛谷 P2296 寻找道路
- 洛谷 P2296 寻找道路
- 洛谷 P2296 寻找道路
- 洛谷 P2296 寻找道路
- 洛谷P2296 寻找道路
- 洛谷 P2296 寻找道路
- 洛谷 P2296 寻找道路
- 洛谷 [P2296] 寻找道路
- NOIP2014 day2 T2 洛谷P2296 寻找道路
- 洛谷 P2296 [NOIP2014 D2T2] 寻找道路
- |洛谷|搜索|NOIP2014|P2296 寻找道路
- 洛谷P2296 寻找道路(spfa)
- 洛谷P2296 寻找道路(NOIp2014)
- P2296 寻找道路
- Luogu P2296 [NOIP2014]寻找道路
- 最短路——洛谷P2296 寻找道路
- 洛谷P2296
- 使用python实现的DI原型
- spark性能调优二--JVM调优
- Timer and TimerTask
- 《Oracle相关文章索引(1)》
- TIM_OCPolarity pwm
- 洛谷 P2296 寻找道路
- 法律裁判文书的统计结果
- 分布式文件系统 FastDFS
- Apache POI操作Word概览
- 请求返回字符串的
- 进程保活
- 超稳定Expandablelistview,带优化,带联网
- VisualVM通过jstatd方式远程监控远程主机
- webstorm 下载并设置jade、less