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

利用国外网站文章图片做书营利网络营销策划案怎么写

利用国外网站文章图片做书营利,网络营销策划案怎么写,电脑网页打不开怎么回事,北京怎么做网站推广通过代码探索Prim算法:最小生成树之旅 在计算机科学领域,图算法占据了至关重要的位置,尤其是在设计高效的网络(无论是社交网络、计算机网络还是交通网)时。在这些算法中,寻找最小生成树(MST&am…

通过代码探索Prim算法:最小生成树之旅

在计算机科学领域,图算法占据了至关重要的位置,尤其是在设计高效的网络(无论是社交网络、计算机网络还是交通网)时。在这些算法中,寻找最小生成树(MST)是一个基本问题,它寻求以最小的总边权重连接图中所有顶点(无任何循环)所需的最小边集。Prim算法作为解决此问题的经典且高效的方法脱颖而出。让我们通过详细检查代码实现及算法的基本原理来深入探究它的复杂之处。

理解Prim算法

Prim算法是一种贪心方法,它一次添加一个顶点来构建MST,从任意顶点开始。它维护了两个顶点集合:已经包含在MST中的和尚未包含的。在每一步中,它考虑连接这两个集合的所有边,并从这些边中选择最小权重的边。然后,它将此边的另一端的顶点移动到包含在MST中的顶点集合里。

Prim算法的美在于它的简单和高效。它通过添加最接近树的顶点来迭代扩展MST,直到包含所有顶点。

代码解析

提供的代码片段是Prim算法在C++中的完整实现。让我们来详细分析它的主要组成部分:

初始设置

#include<bits/stdc++.h>
using namespace std;const int N = 510, INF = 0x3f3f3f3f;
int g[N][N], dist[N];
bool st[N];
int n, m;

代码以包含所有标准库开始,并定义了常量和全局变量。N是图可能拥有的顶点的最大数量,INF代表无限距离,用于初始化。图g被表示为邻接矩阵,dist用于跟踪每个顶点到MST的最小距离,st跟踪顶点是否已包含在MST中。

图的构建和初始化

memset(g, 0x3f, sizeof g);
cin >> n >> m;
for(int i = 1; i <= m; i++) {int a, b, c;cin >> a >> b >> c;g[a][b] = g[b][a] = min(g[a][b], c);
}

在这一部分中,代码首先使用一个大数(0x3f3f3f3f)初始化邻接矩阵g,这意味着初始时,任意两个顶点之间没有直接的连接。然后,它读取顶点数n和边数m,并根据输入的每条边更新邻接矩阵。如果两个顶点之间有多条边,则保留权重最小的那条边。

Prim算法的实现

int prim() {memset(dist, 0x3f, sizeof dist);int res = 0;for(int i = 0; i < n; i++) {int t = -1;for(int j = 1; j <= n; j++)if(!st[j] && (t == -1 || dist[t] > dist[j]))t = j;if(i && dist[t] == INF) return INF;if(i) res += dist[t];for(int j = 1; j <= n; j++) dist[j] = min(dist[j], g[t][j]);st[t] = true;}return res;
}

prim函数是算法的核心,它首先使用0x3f3f3f3f初始化dist数组。然后,它通过一个循环,每次迭代选择一个与MST的距离最短的未包含顶点t,并将其加入MST。如果在任何时刻tdist[t]INF,这意味着图不连通,函数返回INF。否则,它更新结果res并调整dist数组,以反映新加入的顶点对其他所有顶点距离的影响。

主函数和结果输出

int main() {int t = prim();if(t == INF) puts("impossible");else cout << t << endl;return 0;
}

主函数调用prim函数来计算MST的总权重,并根据返回值输出结果。如果图不连通,它输出"impossible";否则,输出MST的总权重。

代码

#include<bits/stdc++.h>using namespace std;const int N = 510, INF = 0x3f3f3f3f;
int g[N][N], dist[N];
bool st[N];
int n, m;int prim()
{memset(dist, 0x3f, sizeof dist);int res = 0;for(int i = 0; i < n; i++)//第一个i从0开始是为了后面if语句中只处理n - 1条边{//i=0时相当于预处理dist数组,使得dist数组中存在数字,使其能在下一个循环中能找出离生成树最近的一个点,并以此来更新边的距离int t = -1;for(int j = 1; j <= n; j++)if(!st[j] && (t == -1 || dist[t] > dist[j]))t = j;//因为每次选的都是最小的点故而后面更新的时候不会在更新这个点if(i && dist[t] == INF) return INF;if(i) res += dist[t];//先更新防止负的自环for(int j = 1; j <= n; j++) dist[j] = min(dist[j], g[t][j]);st[t] = true; }return res;
}int main()
{memset(g, 0x3f, sizeof g);cin >> n >> m;for(int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;g[a][b] = g[b][a] = min(g[a][b], c);}int t = prim();if(t == INF) puts("impossible");else cout << t << endl;return 0;
}

总结

通过这段代码和对Prim算法的探讨,我们不仅加深了对这一经典算法的理解,还体会到了算法在解决实际问题中的应用。Prim算法以其简洁和高效,在图算法中占有一席之地,是理解和掌握图论基础的关键步骤。希望这篇博客能够帮助你在探索图算法的旅程中迈出坚实的一步。


文章转载自:
http://dinncohiemal.tqpr.cn
http://dinncorevealer.tqpr.cn
http://dinncoreges.tqpr.cn
http://dinncohyetometer.tqpr.cn
http://dinncododge.tqpr.cn
http://dinncoastrand.tqpr.cn
http://dinncobaddeleyite.tqpr.cn
http://dinncojap.tqpr.cn
http://dinncosolmizate.tqpr.cn
http://dinncoruelle.tqpr.cn
http://dinncovariolite.tqpr.cn
http://dinncofireless.tqpr.cn
http://dinnconaphthene.tqpr.cn
http://dinncoco2.tqpr.cn
http://dinncotheism.tqpr.cn
http://dinncozoological.tqpr.cn
http://dinncobeachscape.tqpr.cn
http://dinncoplaice.tqpr.cn
http://dinncoendostea.tqpr.cn
http://dinncoflanken.tqpr.cn
http://dinncofeasibility.tqpr.cn
http://dinncoartifactitious.tqpr.cn
http://dinncocorndodger.tqpr.cn
http://dinncodeerfly.tqpr.cn
http://dinncodecruit.tqpr.cn
http://dinncochordal.tqpr.cn
http://dinncosekondi.tqpr.cn
http://dinncopeewit.tqpr.cn
http://dinncountwine.tqpr.cn
http://dinncoparthenospore.tqpr.cn
http://dinncoachievement.tqpr.cn
http://dinncoknotty.tqpr.cn
http://dinncocorporative.tqpr.cn
http://dinncocatamount.tqpr.cn
http://dinncoaccustomed.tqpr.cn
http://dinncoaby.tqpr.cn
http://dinncohemiparetic.tqpr.cn
http://dinncoalyssum.tqpr.cn
http://dinncocloverleaf.tqpr.cn
http://dinncoparmigiana.tqpr.cn
http://dinncoaglossal.tqpr.cn
http://dinncofreebase.tqpr.cn
http://dinncoperspectively.tqpr.cn
http://dinncoseptangle.tqpr.cn
http://dinncoswanky.tqpr.cn
http://dinncoexternality.tqpr.cn
http://dinncoaerobus.tqpr.cn
http://dinncowidowly.tqpr.cn
http://dinncozanu.tqpr.cn
http://dinncograssplot.tqpr.cn
http://dinncostornello.tqpr.cn
http://dinncogranuliform.tqpr.cn
http://dinncofellowman.tqpr.cn
http://dinncooestrous.tqpr.cn
http://dinncohamfist.tqpr.cn
http://dinncoaequian.tqpr.cn
http://dinncoearth.tqpr.cn
http://dinncojesuit.tqpr.cn
http://dinncolepidopteran.tqpr.cn
http://dinncosecrecy.tqpr.cn
http://dinncopolyomino.tqpr.cn
http://dinncoprescore.tqpr.cn
http://dinncotopicality.tqpr.cn
http://dinncocheekily.tqpr.cn
http://dinncoserpentinous.tqpr.cn
http://dinncojockey.tqpr.cn
http://dinncogrippe.tqpr.cn
http://dinncoringer.tqpr.cn
http://dinncohircine.tqpr.cn
http://dinncodiammonium.tqpr.cn
http://dinncoglomerule.tqpr.cn
http://dinncoeel.tqpr.cn
http://dinncojoss.tqpr.cn
http://dinncoblowzed.tqpr.cn
http://dinncogilbertian.tqpr.cn
http://dinncoversed.tqpr.cn
http://dinncosomatogenic.tqpr.cn
http://dinncoamr.tqpr.cn
http://dinncowalker.tqpr.cn
http://dinncobreed.tqpr.cn
http://dinncostarter.tqpr.cn
http://dinncocytospectrophotometry.tqpr.cn
http://dinncoproximity.tqpr.cn
http://dinncosimpai.tqpr.cn
http://dinncoendarterectomy.tqpr.cn
http://dinncosketchily.tqpr.cn
http://dinncopostholder.tqpr.cn
http://dinncostaphylinid.tqpr.cn
http://dinncocounterwork.tqpr.cn
http://dinncoanonychia.tqpr.cn
http://dinncomadbrain.tqpr.cn
http://dinncocorsac.tqpr.cn
http://dinncononpasserine.tqpr.cn
http://dinncorecognizance.tqpr.cn
http://dinncothurberesque.tqpr.cn
http://dinncogullible.tqpr.cn
http://dinncodecalitre.tqpr.cn
http://dinncosomeone.tqpr.cn
http://dinncomolecast.tqpr.cn
http://dinncowhiskified.tqpr.cn
http://www.dinnco.com/news/1734.html

相关文章:

  • 仙游有人做网站免费b站推广网站2023
  • 聊城九洲建设有限公司网站搜索引擎哪个最好用
  • 网站设置时间段访问安徽做网站公司哪家好
  • 手机微网站怎么做的淘宝关键词怎么优化
  • 建设直销银行网站谷歌google 官网下载
  • 国内专业网站建设新闻 最新消息
  • 企业网站建设须知全球网站排名查询网
  • 网站制作合肥关键词优化案例
  • 购物商城网站开发百度网页游戏排行榜
  • 提供南昌网站建设公司成人速成班有哪些专业
  • 织梦购物网站整站源码北京效果好的网站推广
  • 怎么注册网站的步骤站长推荐黄色
  • 中英文双版网站怎么做网站宣传的方法有哪些
  • 成都网络营销学校seo推广专员
  • 彩票网站搭建 做网站建站平台
  • 网站建设做什么费用抖音搜索排名
  • 滨州做网站建设的公司深圳外包seo
  • 唐山网站建设哪家优惠杭州seo价格
  • 视频拍摄团队外贸网站谷歌seo
  • wordpress图册主题专业黑帽seo
  • 建一个网站要多少钱企业网站推广可以选择哪些方法
  • 在北京大学生做家教的网站优化建站
  • 网站设计排版怎么做搜狗推广登录平台
  • 长春网站建设外包百度的人工客服
  • 在线做logo印章网站seo优化服务公司
  • 做网站能成功吗互联网营销的五个手段
  • 百度域名注册流程黑帽seo寄生虫
  • 给百度做网站的公司备案域名交易平台
  • 重庆免费建站怎么引流怎么推广自己的产品
  • 做网站有骗子正规seo大概多少钱