博客
关于我
数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
阅读量:617 次
发布时间:2019-03-12

本文共 1185 字,大约阅读时间需要 3 分钟。

????????????????1????n????????????????????????BFS???????BFS??????????????

????

  • ??????????1?????n??????????????????????????????BFS???????
  • ??????????????????????????????
  • ???????BFS??????1???????????????????n?????BFS????????????????
  • ???????????????????????BFS???????????????
  • ????

    #include 
    #include
    #include
    using namespace std;int main() { int k; // ?????? cin >> k; for (int test = 0; test < k; ++test) { int n, m; cin >> n >> m; vector
    > graph(n + 1); vector
    distance(n + 1, -1); queue
    q; distance[1] = 0; q.push(1); bool found = false; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : graph[u]) { if (distance[v] == -1) { distance[v] = distance[u] + 1; if (v == n) { found = true; break; } q.push(v); } } if (found) break; } if (distance[n] == -1) { cout << "NO" << endl; } else { cout << distance[n] << endl; } } return 0;}

    ????

  • ???????????????????????????
  • ???????????????graph???????????????distance????????????
  • BFS????1???????????????n?????????
  • ?????????n???????????????NO?
  • ??????????????????????????????

    转载地址:http://pkexz.baihongyu.com/

    你可能感兴趣的文章
    python | 高效使用Python工具自动生成模块文档的秘诀
    查看>>
    python 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    Python 中只有一个 True 和一个 False 对象吗?
    查看>>
    python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
    查看>>
    Python 中多线程与多处理之间的区别
    查看>>
    Python 中如何使用 lambda 函数
    查看>>
    Python 中如何创建多行字符串?
    查看>>
    Python 中如何处理异常?
    查看>>
    Python 中如何实现列表的切片?
    查看>>
    Python 中的装饰器是什么?
    查看>>
    Python 中的装饰器是如何工作的,有哪些实际应用场景?
    查看>>
    python请求html_使用Python请求获取HTML?
    查看>>
    Python 之匿名函数和偏函数
    查看>>
    python 之栈的实现
    查看>>