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

    你可能感兴趣的文章
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>
    multiprocessing.pool.map 和带有两个参数的函数
    查看>>
    MYSQL CONCAT函数
    查看>>
    multiprocessing.Pool:map_async 和 imap 有什么区别?
    查看>>
    MySQL Connector/Net 句柄泄露
    查看>>
    multiprocessor(中)
    查看>>
    mysql CPU使用率过高的一次处理经历
    查看>>