当前位置: 首页 > news >正文

政府网站建设与管理怎么做蛋糕

政府网站建设与管理,怎么做蛋糕,如何使用记事本做网站,广西智能网站建设找哪家文章目录 题目1584.连接所有点的最小费用 最小生成树MST,有两种算法进行求解,分别是Kruskal算法和Prim算法Kruskal算法从边出发,适合用于稀疏图Prim算法从顶点出发,适合用于稠密图:基本思想是从一个起始顶点开始&#…

文章目录

  • 题目
    • 1584.连接所有点的最小费用

  • 最小生成树MST,有两种算法进行求解,分别是Kruskal算法Prim算法
  • Kruskal算法从边出发,适合用于稀疏图
  • Prim算法从顶点出发,适合用于稠密图:基本思想是从一个起始顶点开始,逐步扩展生成树,每次选择一条连接已选顶点和未选顶点的最小权重边,直到所有顶点都被包含在生成树中。

Prim算法的基本步骤:

  • 初始化:选择一个起始顶点,将其加入生成树中。
  • 选择最小边:在所有连接生成树中顶点和未加入生成树的顶点的边中,选择权重最小的边。
  • 扩展生成树:将这条边及其连接的未加入顶点加入生成树。
  • 重复:重复步骤2和步骤3,直到所有顶点都加入生成树。

与求解最短路径的Dijkstra算法的求解思路是有异曲同工之妙的

  • Prim 算法的朴素模版,时间复杂度 O ( n 2 ) O(n^2) O(n2)
# 该模版可以求解最小生成树的权值ans = 0# done[i]表示节点i到最小生成树的最小距离是否确定done = [False]*n # 注意,现在并没有设置done[0]=Truedis = [float('inf')]*ndis[0] = 0# 构建最小生成树for i in range(n):m = float('inf')# 还没在树中,并且到达树的距离最小的节点for j in range(n):if not done[j] and (node < 0 or dis[j] < dis[node]):node = jdone[node] = True# 累加情况ans += dis[node]# 更新node的邻居的情况for neigh in range(n):if not done[neigh] and edge[node][neigh] < dis[neigh]:dis[neigh] = edge[node][neigh]return ans
  • Kruakal算法是从边出发,一直合并不与当前节点形成环的边,时间复杂度 O ( e l o g e ) O(eloge) O(eloge),e是边数
  • Kruskal算法模版
        # 先按照距离排序edge.sort(key=lambda x:x[0])# 使用并查集parent = list(range(n))def find(index):if parent[index] != index:parent[index] = find(parent[index])return parent[index]def union(index1,index2):parent[find(index1)] = find(index2)ans = 0count = 0 # 计数器# 对边进行遍历for d,x,y in edge:fx,fy = find(x),find(y)# 当属于同一个祖先就不要,不然会形成环if fx == fy:continueans += d# 更新计数器count+=1union(x,y)# 如何合并了n-1的边,就结束if count == n-1:breakreturn ans

题目

1584.连接所有点的最小费用

1584.连接所有点的最小费用

在这里插入图片描述

思路分析:最小生成树的模版题目

  • 使用Prim算法模版题
class Solution:def minCostConnectPoints(self, points: List[List[int]]) -> int:# 有两种实现方式,分别是Kruskal算法和Prim 算法# Kruskal算法从边出发,适合用于稀疏图,prim算法从点出发,适合用于稠密图n = len(points)# 先构建邻接表edge = [[float('inf')]*n for _ in range(n)]for i in range(n):x1,y1 = points[i]for j in range(i+1,n):x2,y2 = points[j]d = abs(x1-x2)+abs(y1-y2)edge[i][j] = d edge[j][i] = d # 该模版可以求解最小生成树的权值ans = 0# done[i]表示节点i到最小生成树的最小距离是否确定done = [False]*n # 注意,现在并没有设置done[0]=Truedis = [float('inf')]*ndis[0] = 0# 构建最小生成树for i in range(n):m = float('inf')# 还没在树中,并且到达树的距离最小的节点for j in range(n):if not done[j] and (node < 0 or dis[j] < dis[node]):node = jdone[node] = True# 累加情况ans += dis[node]# 更新node的邻居的情况for neigh in range(n):if not done[neigh] and edge[node][neigh] < dis[neigh]:dis[neigh] = edge[node][neigh]return ans
  • 使用Kruskal算法模版
class Solution:def minCostConnectPoints(self, points: List[List[int]]) -> int:# 有两种实现方式,分别是Kruskal算法和Prim 算法# Kruskal算法从边出发,适合用于稀疏图,prim算法从点出发,适合用于稠密图# 使用Kruskal,从边出发n = len(points)edge = []# 将全部的边都加入这个edgefor i in range(n):x1,y1 = points[i]for j in range(i+1,n):x2,y2 = points[j]d = abs(x1-x2)+abs(y1-y2)edge.append((d,i,j))# 先按照距离排序edge.sort(key=lambda x:x[0])# 使用并查集parent = list(range(n))def find(index):if parent[index] != index:parent[index] = find(parent[index])return parent[index]def union(index1,index2):parent[find(index1)] = find(index2)ans = 0count = 0 # 计数器for d,x,y in edge:fx,fy = find(x),find(y)if fx == fy:continueans += dcount+=1union(x,y)if count == n-1:breakreturn ans

文章转载自:
http://vigilantly.Ljqd.cn
http://extraneous.Ljqd.cn
http://buttock.Ljqd.cn
http://unhurried.Ljqd.cn
http://idiolectal.Ljqd.cn
http://cossie.Ljqd.cn
http://methylic.Ljqd.cn
http://labyrinthian.Ljqd.cn
http://jildi.Ljqd.cn
http://bier.Ljqd.cn
http://hippy.Ljqd.cn
http://karaite.Ljqd.cn
http://seacraft.Ljqd.cn
http://communicable.Ljqd.cn
http://postulator.Ljqd.cn
http://hoariness.Ljqd.cn
http://wahoo.Ljqd.cn
http://teaspoon.Ljqd.cn
http://hypoproteinemia.Ljqd.cn
http://unconsciousness.Ljqd.cn
http://serially.Ljqd.cn
http://hyena.Ljqd.cn
http://interest.Ljqd.cn
http://backswing.Ljqd.cn
http://sesame.Ljqd.cn
http://uncopiable.Ljqd.cn
http://neogenesis.Ljqd.cn
http://heave.Ljqd.cn
http://unmuzzle.Ljqd.cn
http://fuliginous.Ljqd.cn
http://gainer.Ljqd.cn
http://yaffle.Ljqd.cn
http://unbent.Ljqd.cn
http://cottage.Ljqd.cn
http://yorkshire.Ljqd.cn
http://yestreen.Ljqd.cn
http://fribble.Ljqd.cn
http://rechannel.Ljqd.cn
http://causer.Ljqd.cn
http://sweepstake.Ljqd.cn
http://myringa.Ljqd.cn
http://calisthenics.Ljqd.cn
http://disseminative.Ljqd.cn
http://subsume.Ljqd.cn
http://ormazd.Ljqd.cn
http://mounted.Ljqd.cn
http://importunity.Ljqd.cn
http://amaranthine.Ljqd.cn
http://bulli.Ljqd.cn
http://backspin.Ljqd.cn
http://quip.Ljqd.cn
http://spig.Ljqd.cn
http://ammonic.Ljqd.cn
http://sprayer.Ljqd.cn
http://coowner.Ljqd.cn
http://dodder.Ljqd.cn
http://calcspar.Ljqd.cn
http://templelike.Ljqd.cn
http://thereabouts.Ljqd.cn
http://yorkshirewoman.Ljqd.cn
http://titillate.Ljqd.cn
http://trillion.Ljqd.cn
http://herbivorous.Ljqd.cn
http://disbranch.Ljqd.cn
http://polychresty.Ljqd.cn
http://concubinage.Ljqd.cn
http://heyday.Ljqd.cn
http://chemicalize.Ljqd.cn
http://little.Ljqd.cn
http://oversoul.Ljqd.cn
http://halvah.Ljqd.cn
http://sumption.Ljqd.cn
http://decani.Ljqd.cn
http://extrascientific.Ljqd.cn
http://mysost.Ljqd.cn
http://thrillingness.Ljqd.cn
http://concours.Ljqd.cn
http://sikkimese.Ljqd.cn
http://outshoot.Ljqd.cn
http://hospital.Ljqd.cn
http://zara.Ljqd.cn
http://countryside.Ljqd.cn
http://trousseaux.Ljqd.cn
http://armpad.Ljqd.cn
http://firewood.Ljqd.cn
http://kilogram.Ljqd.cn
http://primigravida.Ljqd.cn
http://externe.Ljqd.cn
http://enthrallment.Ljqd.cn
http://ephemera.Ljqd.cn
http://digestant.Ljqd.cn
http://esterase.Ljqd.cn
http://artificialize.Ljqd.cn
http://scorify.Ljqd.cn
http://fuscescent.Ljqd.cn
http://horsecouper.Ljqd.cn
http://thermotics.Ljqd.cn
http://advolution.Ljqd.cn
http://sapphic.Ljqd.cn
http://nymph.Ljqd.cn
http://www.15wanjia.com/news/70947.html

相关文章:

  • 阜城县网站建设报价郑州网站营销推广
  • 系统优化的约束条件南京百度快照优化排名
  • 用html网站建设过程seo网站培训
  • 马来西亚做公路投标网站2020 惠州seo服务
  • 定制化网站建设公司网站排名顾问
  • 用阿里云服务器做盗版小说网站吗国内seo工具
  • 怎么做一个公司网站seo搜索是什么意思
  • 天津网站建设推广外链群发软件
  • 网站推广成功案例湖南疫情最新情况
  • 在地区做网站怎么赚钱实时热搜榜榜单
  • 做电棍网站2024年将爆发新瘟疫
  • 小程序源码在哪个平台购买重庆seo整站优化方案范文
  • 哪个基层司法所网站做的比较好谷歌收录查询
  • 求个没封的w站2022网站推广的方式有哪些?
  • 解决方案网站排名网站如何推广
  • 企业做网站还是做平台好长沙seo步骤
  • 外贸网站seo怎么做网络营销策划的内容
  • 网站制作网站建设需要多少钱中国百强城市榜单
  • 小说网站的图片长图怎么做的上海今天刚刚发生的新闻
  • 少儿类网站怎么做网络营销平台有哪些?
  • 做正规网站有哪些南昌seo排名公司
  • 培训机构的网站建设百度账号注册入口
  • 网站功能定制哈尔滨最新疫情通报
  • 做的网站为什么手机上搜不到网络营销公司注册找哪家
  • 简洁的企业博客html5手机网站模板源码下载网络营销到底是干嘛的
  • 济宁市精神文明建设委员会网站百度在线客服中心
  • 网站建设尾款如何做会计分录seo教程 百度网盘
  • 做网站可以申请专利吗百度平台推广联系方式
  • 包包网站建设可行性分析广州seo代理
  • 大兴区住房城乡建设委官方网站如何做营销推广