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

    你可能感兴趣的文章
    multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
    查看>>
    mysql csv import meets charset
    查看>>
    multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
    查看>>
    MySQL DBA 数据库优化策略
    查看>>
    multi_index_container
    查看>>
    mutiplemap 总结
    查看>>
    MySQL Error Handling in Stored Procedures---转载
    查看>>
    MVC 区域功能
    查看>>
    MySQL FEDERATED 提示
    查看>>
    mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
    查看>>
    Mysql group by
    查看>>
    MySQL I 有福啦,窗口函数大大提高了取数的效率!
    查看>>
    mysql id自动增长 初始值 Mysql重置auto_increment初始值
    查看>>
    MySQL in 太多过慢的 3 种解决方案
    查看>>
    Mysql Innodb 锁机制
    查看>>
    MySQL InnoDB中意向锁的作用及原理探
    查看>>
    MySQL InnoDB事务隔离级别与锁机制深入解析
    查看>>
    Mysql InnoDB存储引擎 —— 数据页
    查看>>
    Mysql InnoDB存储引擎中的checkpoint技术
    查看>>
    Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
    查看>>