博客
关于我
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/

    你可能感兴趣的文章
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    Orleans框架------基于Actor模型生成分布式Id
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    ORM框架 和 面向对象编程
    查看>>
    OS X Yosemite中VMware Fusion实验环境的虚拟机文件位置备忘
    查看>>
    os.environ 没有设置环境变量
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.removexattr 的 Python 文档——‘*‘(星号)参数是什么意思?
    查看>>
    os.system 在 Python 中不起作用
    查看>>
    OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
    查看>>
    OSCACHE介绍
    查看>>
    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
    查看>>
    OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
    查看>>
    SQL--mysql索引
    查看>>