博客
关于我
数据结构实验之图论五:从起始点到目标点的最短步数(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/

    你可能感兴趣的文章
    Poj1459 Power Network 预流推进
    查看>>
    POJ1502(MPI Maelstrom)
    查看>>
    poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]
    查看>>
    poj1730 - Perfect Pth Powers(完全平方数)(水题)
    查看>>
    poj1753——Flip Game
    查看>>
    poj1936 假期计划第一水
    查看>>
    poj1958-汉诺四塔问题(三种方法)
    查看>>
    poj1988(并查集)
    查看>>
    POJ2007+几何+极角排序
    查看>>
    poj2039
    查看>>
    poj2135(简单的最小费用流问题)
    查看>>
    poj2195 bfs+最小权匹配
    查看>>
    POJ2251
    查看>>
    POJ2253-Frogger
    查看>>
    poj2309
    查看>>
    POJ2390 Bank Interest【水题】
    查看>>
    poj2398
    查看>>
    poj2478欧拉函数
    查看>>
    poj2546
    查看>>
    POJ2728 Desert King
    查看>>