栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2022深圳北B题1问python代码

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022深圳北B题1问python代码

Prim完成,未上传造价计算代码。附简易拓扑图,征队友。

import math
import turtle
import numpy as np


class Node:
    x = y = 0
    demand = 0
    name = ''
    father = None
    next = None
    e_length = 0
    out_degree = 0
    is_switch = False
    state = True  # True for work well

    def __init__(self, x, y, name):
        self.x = x
        self.y = y
        self.name = name


class Graph:
    arc_node = list()


class Link:
    head = None
    tail = None
    data = 1e05
    next = None

    def __init__(self, head=None, tail=None, data=1E05, nex=None):
        self.head = head
        self.tail = tail
        self.data = data
        self.next = nex


def copy1(self):
    return Link(self.head, self.tail, self.data, None)


def distance(node1: Node, node2: Node):  # calculate the distance of two nodes
    return math.sqrt((node1.x - node2.x) ** 2 + (node1.y - node2.y) ** 2)


def draw(graph):
    turtle.penup()
    turtle.screensize(200, 200)
    for node in graph:
        turtle.goto(node.x / 4, node.y / 4)
        turtle.pendown()
        turtle.dot(10)
        turtle.penup()


def draw_graph(T):
    T1 = set()
    turtle.penup()
    turtle.screensize(2000, 2000)
    for node in T:
        node_p = node
        turtle.goto(node_p.x, node_p.y)
        turtle.pendown()
        while node_p:

            turtle.goto(node_p.x, node_p.y)
            turtle.dot(40, "red")
            if node_p in T1:
                break
            else:
                T1.add(node_p)
            node_p = node_p.father
        turtle.penup()
    turtle.done()


def creat_link(graph):
    head = Link()
    p = Link()
    tail = p
    for m in range(len(graph)):
        for n in range(m + 1, len(graph)):
            temp = Link(graph[m], graph[n], distance(graph[m], graph[n]), p)
            p = temp

    return p


def sort(head: Link):
    start = Link()
    p = start
    p.data = 1.11
    p2 = head
    while p2:
        p3 = head
        temp = Link()
        while p3:
            if temp.data > p3.data > p.data:
                temp = p3
            p3 = p3.next
        p.next = copy1(temp)
        p = p.next
        p2 = p2.next
    return start


'''
def sort(head: Link):
    p = last = head
    while p:
        p2 = last2 = head
        while p2 != p:
            if p2.data > p.data:
                last.next = p.next
                last2.next = p
                p.next = p2
                p = last.next
                break
            last2 = p2
            p2 = p2.next
        if p2 == p:
            last = p
            p = p.next
        print(p.data)
        print(p2.data)
        print()
    return head
'''

'''
def prim(graph, e_head: Link, num_v):

    p = e_head.next
    set1 = {p.head, p.tail}

    turtle.screensize(2000, 2500)
    turtle.penup()
    turtle.goto(p.head.x, p.head.y)
    turtle.pendown()
    turtle.dot(40, "red")
    turtle.goto(p.tail.x, p.tail.y)
    turtle.dot(40, "red")

    while len(set1) != num_v:
        while p:
            if p.tail in set1 and p.head not in set1 
                    or p.tail not in set1 and p.head in set1:
                head = p.head if p.head in set1 else p.tail  # judge which is head node
                tail = p.tail if head == p.head else p.head
                head.next = tail

                turtle.penup()
                turtle.goto(head.x, head.y)
                turtle.pendown()
                turtle.goto(tail.x, tail.y)
                turtle.dot(40, "red")

                set1.add(tail)
                p = e_head.next
                break
            p = p.next
    #turtle.done()
'''


def prim(graph, e_head: Link, num_v):

    p = e_head.next
    set1 = {p.head, p.tail}
    while len(set1) != num_v:
        while p:
            if p.tail in set1 and p.head not in set1 
                    or p.tail not in set1 and p.head in set1:
                head = p.head if p.head in set1 else p.tail  # judge which is head node
                tail = p.tail if head == p.head else p.head
                tail.father = head
                set1.add(tail)
                p = e_head.next
                break
            p = p.next


def creat_T(graph):
    T = set()
    T1 = set()
    for node in graph:
        father = node.father
        while father:
            father = father.father
            if father not in T1:
                T1.add(father)
    for node in graph:
        if node not in T1 :
            T.add(node)
    return T


if __name__ == '__main__':
    size = 48
    X = np.zeros((size, 4))
    t = range(len(X))
    X[:, 0] = t
    X[:, 1:] = np.random.rand(size, 3) * 1000
    graph1 = list()
    for k in range(len(X)):
        graph1.append(Node(X[k, 1], X[k, 2], X[k, 0]))
    l_head = creat_link(graph1)
    l_head = sort(l_head)
    prim(graph1, l_head, size)
    T0 = creat_T(graph1)
    draw_graph(T0)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号