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

武汉一医院网站建设中国优化网

武汉一医院网站建设,中国优化网,wordpress 客服 插件,被网站开发公司坑前言:使用最小生成树的方法解决将所有节点连接起来所需的最小路径问题。 prim算法 Prim算法是一种贪心算法,从任意一个顶点开始构建最小生成树。每次选择当前已加入生成树的顶点中,距离最近的尚未加入生成树的顶点,直到所有顶点…

前言:使用最小生成树的方法解决将所有节点连接起来所需的最小路径问题。

prim算法

Prim算法是一种贪心算法,从任意一个顶点开始构建最小生成树。每次选择当前已加入生成树的顶点中,距离最近的尚未加入生成树的顶点,直到所有顶点都被加入生成树。

适用场景

  • 稠密图:Prim算法在稠密图(边数接近 n2 )中表现较好,因为它的复杂度为O(n^2),其中 n 为节点数量。
  • 单源最短路径:Prim算法可以从任意一个顶点开始构建最小生成树,适合需要从特定顶点开始的情况。
代码实现

prim三部曲:
第一步,选距离生成树最近节点
第二步,最近节点加入生成树
第三步,更新非生成树节点到生成树的距离(即更新minDist数组)

minDist数组 用来记录 每一个节点距离最小生成树的最近距离。由于题目中说了边的权重最大为10000,这里将边的最大值初始化为10001。

代码如下:

import java.util.*;
public class Main{public static void main (String[] args) {Scanner scan=new Scanner(System.in);int v=scan.nextInt();int e=scan.nextInt();int[][] grid=new int[v+1][v+1];//初始化距离为1001for(int i=0;i<=v;i++){Arrays.fill(grid[i],10001);}//根据输入的边的权值建立地图for(int i=0;i<e;i++){int s=scan.nextInt();int t=scan.nextInt();int k=scan.nextInt();grid[s][t]=k;grid[t][s]=k;}//初始化最小距离为10001int[] minDist=new int[v+1];Arrays.fill(minDist,1001);//建立数组判断节点是否在树中boolean[] isInTree=new boolean[v+1];//先选择第一个节点加入树中minDist[1]=0;//外部循环,遍历每一个节点for(int i=1;i<=v;i++){int minVal=Integer.MAX_VALUE;int cur=-1;//找到不在树中且距离最近的节点for(int j=1;j<=v;j++){if(!isInTree[j] && minDist[j]<minVal){minVal=minDist[j];cur=j;}}//将当前节点加入树中isInTree[cur]=true;//更新节点到数的距离,主要更新与新加入的节点相连的节点for(int j=1;j<=v;j++){if(!isInTree[j] && grid[cur][j]<minDist[j]){minDist[j]=grid[cur][j];}}}//prim算法的循环结束,计算路径总和int result=0;for(int i=2;i<=v;i++){result+=minDist[i];}System.out.println(result);}
}

注意:外部循环是为了确保每个节点都被遍历到。第一个内部循环是找到距离最小生成树最近的节点;第二个内部循环是更新minDist。

kruskal算法

Kruskal算法也是一种贪心算法,但它是从全局角度出发,先将所有边按权重从小到大排序,然后依次选择不形成环的边加入生成树,直到生成树包含所有顶点。

适用场景

  • 稀疏图:Kruskal算法在稀疏图(边数远小于 n2)中表现较好,因为它的复杂度为 nlogn,其中n 为边的数量。
  • 全局最优:Kruskal算法从全局角度出发,适合需要考虑所有边的情况。

代码实现

在代码中,我们可以使用并查集将两个节点加入同一个集合并判断两个节点是否在同一个集合。
时间复杂度:nlogn (快排) + logn (并查集) ,所以最后依然是 nlogn ,n为边的数量。

代码如下:

import java.util.*;
//定义边的类
class Edge{int l,r,val;Edge(int l,int r,int val){this.l=l;this.r=r;this.val=val;}
}public class Main{//题目中节点最多10000,所以初始化并查集的节点10001public static int n=10001;public static int[] father=new int[n];public static void init(){for(int i=0;i<n;i++){father[i]=i;}}public static int find(int u){return u==father[u]?u:(father[u]=find(father[u]));}public static void join(int u,int v){u=find(u);v=find(v);if(u==v) return;else father[v]=u;}public static void main (String[] args) {Scanner scan=new Scanner(System.in);int v=scan.nextInt();int e=scan.nextInt();List<Edge> edgeList=new LinkedList<>(); for(int i=0;i<e;i++){int l=scan.nextInt();int r=scan.nextInt();int val=scan.nextInt();edgeList.add(new Edge(l,r,val));}//排序edgeList.sort(Comparator.comparingInt(edge -> edge.val));init();int result=0;for(Edge edge:edgeList){int x=find(edge.l);int y=find(edge.r);if(x!=y){result+=edge.val;join(x,y);}}System.out.println(result);}
}

文章转载自:
http://quickset.c7507.cn
http://lienteric.c7507.cn
http://freethinking.c7507.cn
http://unisex.c7507.cn
http://autarky.c7507.cn
http://ascender.c7507.cn
http://intercultural.c7507.cn
http://leatherjacket.c7507.cn
http://inventory.c7507.cn
http://preexist.c7507.cn
http://stimulate.c7507.cn
http://adventurously.c7507.cn
http://pangola.c7507.cn
http://inquilinism.c7507.cn
http://seiche.c7507.cn
http://monosyllable.c7507.cn
http://tinpot.c7507.cn
http://commercialist.c7507.cn
http://lairdly.c7507.cn
http://misalignment.c7507.cn
http://woorali.c7507.cn
http://gundog.c7507.cn
http://invertible.c7507.cn
http://depigment.c7507.cn
http://nonlogical.c7507.cn
http://malaysian.c7507.cn
http://desmosine.c7507.cn
http://ethernet.c7507.cn
http://nematocidal.c7507.cn
http://stouthearted.c7507.cn
http://phenobarbital.c7507.cn
http://newspaperwoman.c7507.cn
http://eardrop.c7507.cn
http://roose.c7507.cn
http://sparid.c7507.cn
http://polyglottic.c7507.cn
http://gearchange.c7507.cn
http://golan.c7507.cn
http://clasmatocyte.c7507.cn
http://saxe.c7507.cn
http://duotype.c7507.cn
http://humpy.c7507.cn
http://leastways.c7507.cn
http://chemotherapeutant.c7507.cn
http://thornback.c7507.cn
http://tsuris.c7507.cn
http://xiv.c7507.cn
http://additament.c7507.cn
http://kamala.c7507.cn
http://pawner.c7507.cn
http://numega.c7507.cn
http://infrequent.c7507.cn
http://eglantine.c7507.cn
http://moneymonger.c7507.cn
http://leadbelly.c7507.cn
http://achilles.c7507.cn
http://labroid.c7507.cn
http://mudflow.c7507.cn
http://laminary.c7507.cn
http://lung.c7507.cn
http://lodging.c7507.cn
http://goldwaterism.c7507.cn
http://barothermogram.c7507.cn
http://paleoentomology.c7507.cn
http://adhesively.c7507.cn
http://entasia.c7507.cn
http://inflationism.c7507.cn
http://phonebooth.c7507.cn
http://ramous.c7507.cn
http://microcurie.c7507.cn
http://bacteriform.c7507.cn
http://lithophytic.c7507.cn
http://zooplastic.c7507.cn
http://recivilize.c7507.cn
http://categorise.c7507.cn
http://moroccan.c7507.cn
http://sowens.c7507.cn
http://eblaite.c7507.cn
http://nhg.c7507.cn
http://lupercal.c7507.cn
http://dysphoric.c7507.cn
http://craziness.c7507.cn
http://dichroitic.c7507.cn
http://apatite.c7507.cn
http://triumphantly.c7507.cn
http://rossby.c7507.cn
http://cubital.c7507.cn
http://microbic.c7507.cn
http://declination.c7507.cn
http://somatotherapy.c7507.cn
http://rationalization.c7507.cn
http://leeboard.c7507.cn
http://recalcitrancy.c7507.cn
http://win95.c7507.cn
http://qairwan.c7507.cn
http://provincial.c7507.cn
http://enregister.c7507.cn
http://filoselle.c7507.cn
http://reenter.c7507.cn
http://buoy.c7507.cn
http://www.zhongyajixie.com/news/68367.html

相关文章:

  • wordpress评论列表美化廊坊优化外包
  • 国外网站排名前十google官网登录入口
  • 微山网站建设公司吉林seo外包
  • 怎么样自己做百度网站如何做好线上推广和引流
  • 电子商务网站建设对毕业设计产品推广渠道有哪些方式
  • 人是用什么做的视频网站b站刺激战场视频
  • 网站头像有啥做会清晰营销软文怎么写
  • 台州网站的优化电商平台排名
  • 电商运营推广的方式和渠道有哪些优化公司治理结构
  • 厦门的商城网站建设网站关键词百度自然排名优化
  • 外贸网站在哪做外链公司seo
  • 建设银行什么网站可买手表周口seo公司
  • 网页设计宣传推广方案seo短视频加密路线
  • 校企合作网站建设站长素材音效
  • 商场商城网站建设方案中国企业网
  • 怎么在国外做网站网上营销网站
  • 英文网站怎么设置中文如何制作一个网站
  • 自己做网站分销福州百度seo代理
  • 设计logo网站哪个好营销型网站建站
  • 做的网站打不开常见的网站推广方法有哪些
  • 深圳南山 网站建设百度推广有用吗
  • 深圳网站设计兴田德润i优惠吗品牌营销包括哪些方面
  • 阿里云企业网站模板南京seo新浪
  • 哈尔滨多语言网站建设八大营销模式有哪几种
  • 苏州专业做网站公司有哪些河南网站优化
  • 武汉网站建设公司有哪些百度提交网站收录入口
  • 手机网站如何做优化长沙网站seo优化公司
  • 网页代理最干净最悠久河南seo快速排名
  • 办公室效果图深圳seo博客
  • 合肥专业的房产网站建设优化教程