博客
关于我
201403-4 无线网络 ccf
阅读量:264 次
发布时间:2019-03-01

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

????????????????????????????????????????????????????????????????????????k??????????????????

????

  • ??????????????????????????????????????????????????????????????????k???????????

  • ??????????????????????????????????????????????????r?

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

  • ??????BFS????????????????????????????????k????????????????

  • ????

    import sysfrom collections import dequedef main():    n, m, k, r = map(int, sys.stdin.readline().split())    nodes = []    for _ in range(n):        x, y = map(int, sys.stdin.readline().split())        nodes.append((x, y))    new_nodes = []    for _ in range(m):        x, y = map(int, sys.stdin.readline().split())        new_nodes.append((x, y))        # Encode all nodes    all_nodes = nodes + new_nodes    node_pos = [ (x,y) for (x,y) in all_nodes ]    node_id = [i for i in range(len(all_nodes))]    target_pos = node_pos[1]  # node 1 is the second router    # Create adjacency list    adj = [[] for _ in range(len(all_nodes))]    for i in range(len(all_nodes)):        for j in range(i+1, len(all_nodes)):            x1, y1 = node_pos[i]                x2, y2 = node_pos[j]                dx = x1 - x2                dy = y1 - y2                dist = dx*dx + dy*dy                if dist <= r*r:                    adj[i].append(j)                    adj[j].append(i)        # BFS setup    INF = float('inf')    distance = [ [INF]*(k+1) for _ in range(len(all_nodes)) ]    start_pos = node_id[0]  # position of first router    distance[start_pos][0] = 0    queue = deque()    queue.append( (start_pos, 0) )    found = False    min_trans = INF    while queue:        pos, added = queue.popleft()        if pos == node_id[1]:  # reached the second router            if added <=k:                if distance[pos][added] < min_trans:                    min_trans = distance[pos][added]                    found = True            continue        if distance[pos][added] >= INF:            continue        for neighbor in adj[pos]:            if neighbor < n:  # original node                new_added = added            else:  # new node                if added >= k:                    continue                new_added = added + 1            new_dist = distance[pos][added] + 1            if new_dist < distance[neighbor][new_added]:                distance[neighbor][new_added] = new_dist                queue.append( (neighbor, new_added) )        print(min_trans if found else -1)if __name__ == "__main__":    main()

    ????

  • ????????????????????????????
  • ???????????????????????????
  • BFS????????????????????????????????
  • BFS????????????????????????????
  • ??????????????????
  • ??????????????????????????????????????????????????????

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

    你可能感兴趣的文章
    SpringBoot中集成阿里开源缓存访问框架JetCache实现声明式实例和方法缓存
    查看>>
    pom.xml中提示web.xml is missing and <failonmissingw>...
    查看>>
    Pomelo开发中Web客户端开发API简介
    查看>>
    QT教程2:QT5的体系构架
    查看>>
    PON架构(全光网络)
    查看>>
    PoolingHttpClientConnectionManager原理剖析
    查看>>
    QT教程1:ubuntu18.04安装QT5
    查看>>
    POP-一个点击带有放大还原的动画效果
    查看>>
    POP3 协议在计算机网络中的优缺点
    查看>>
    qt批量操作同类型控件
    查看>>
    Portaudio笔记-WASAPI
    查看>>
    position:fixed失效情况
    查看>>
    Qt开发笔记:QGLWidget、QOpenGLWidget详解及区别
    查看>>
    Position属性四个值:static、fixed、absolute和relative的区别和用法
    查看>>
    POSIX thread编程中关于临界区内条件变量的分析
    查看>>
    POSIX与程序可移植性
    查看>>
    posix多线程有感--自旋锁
    查看>>
    SpringBoot中集成海康威视SDK实现布防报警数据上传/交通违章图片上传并在linux上部署(附示例代码资源)
    查看>>
    POSIX标准和XSI扩展
    查看>>
    post install error,please remove node_moules before retry
    查看>>