博客
关于我
数据结构实验之图论五:从起始点到目标点的最短步数(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 中的for in 遍历生成字典、添加判断条件if
    查看>>
    Python 中的Lock与RLock
    查看>>
    Python 中的range(),arange()函数
    查看>>
    Python 中的内存管理机制
    查看>>
    Python 中的函数包装器:模型运行时和调试
    查看>>
    Python 中的列表理解和 lambda
    查看>>
    Python 中的多层for in嵌套循环使用
    查看>>
    Python 中的多线程(01/4)
    查看>>
    Python 中的多进程(01/2):简介
    查看>>
    Python 中的多进程(02/2):进程之间的通信)
    查看>>
    Python 中的字符串、列表、元组和字典数据类型的特点和使用场景
    查看>>
    Python 中的属性访问器是什么?如何使用 @property 装饰器?
    查看>>
    Python 中的带宽限制
    查看>>
    Python 中的机器学习简介:多项式回归
    查看>>
    python读取grib2数据_用Python加载grib2文件
    查看>>
    Python 中的注意点_s2
    查看>>
    Python读取Excel的几种工具包(附Demo)
    查看>>
    Python 中的生成器是什么?
    查看>>
    Python 中的离线语音转文本
    查看>>
    Python读写json文件的简单实现
    查看>>