博客
关于我
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之索引选择及优化
    查看>>
    mysql之联合查询UNION
    查看>>
    mysql之连接查询,多表连接
    查看>>
    mysql乐观锁总结和实践 - 青葱岁月 - ITeye博客
    查看>>
    mysql也能注册到eureka_SpringCloud如何向Eureka中进行注册微服务-百度经验
    查看>>
    mysql乱码
    查看>>
    Mysql事务。开启事务、脏读、不可重复读、幻读、隔离级别
    查看>>
    MySQL事务与锁详解
    查看>>
    MySQL事务原理以及MVCC详解
    查看>>
    MySQL事务及其特性与锁机制
    查看>>
    mysql事务理解
    查看>>
    MySQL事务详解结合MVCC机制的理解
    查看>>
    MySQL事务隔离级别:读未提交、读已提交、可重复读和串行
    查看>>
    MySQL事务隔离级别:读未提交、读已提交、可重复读和串行
    查看>>
    webpack css文件处理
    查看>>
    mysql二进制包安装和遇到的问题
    查看>>
    MySql二进制日志的应用及恢復
    查看>>
    mysql互换表中两列数据方法
    查看>>
    mysql五补充部分:SQL逻辑查询语句执行顺序
    查看>>
    mysql交互式连接&非交互式连接
    查看>>