题目大意:

给出的案例结果得出步骤,如下图所示,从结点1开始查找,找出的一条路径如绿色部分所标注。(关键处在于连接每条路径所需要的适配器的价格得加上去)

代码实现:

 #include<iostream>
#include<cstdio>
using namespace std;
#define MAX 1000
//注意此处范围得按照题意设置为>=1000,否则会Segmentation Fault
#define MAXCOST 0x7fffffff int graph[MAX][MAX]; int prim(int graph[][MAX], int n)
{
int lowcost[MAX];
int mst[MAX];
int i, j, min, minid, sum = ;
for (i = ; i <= n; i++)
{
lowcost[i] = graph[][i];
mst[i] = ;
}
mst[] = ;
for (i = ; i <= n; i++)
{
min = MAXCOST;
minid = ;
for (j = ; j <= n; j++)
{
if (lowcost[j] < min && lowcost[j] != )
{
min = lowcost[j];
minid = j;
}
}
sum += min;
lowcost[minid] = ;
for (j = ; j <= n; j++)
{
if (graph[minid][j] < lowcost[j])
{
lowcost[j] = graph[minid][j];
mst[j] = minid;
}
}
}
return sum;
} int main()
{
int i, j, k, t, en, n,x, y, cost,pre[];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&pre[i]);
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
graph[i][j] = MAXCOST;
}
}
//构建图G
for(i = ; i <= n; i++){
for(k=;k<=n;k++){
scanf("%d",&graph[i][k]);
graph[i][k]+=pre[i];//直接将每条路径上存在的适配器价格给加到权值里面去即可
graph[i][k]+=pre[k];
}
}
cost = prim(graph, n);
cout <<cost << endl;
}
return ;
}

最小生成树-QS Network(Prim)的更多相关文章

  1. ZOJ1586——QS Network(最小生成树)

    QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...

  2. (最小生成树)QS Network -- ZOJ --1586

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...

  3. ZOJ1586:QS Network (最小生成树)

    QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...

  4. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

  5. 最小生成树 E - QS Network

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  6. ZOJ - 1586 QS Network (Prim)

    ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...

  7. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  8. ZOJ1586 QS Network

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  9. ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;&amp;prim)

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

随机推荐

  1. confluence搭建破解及汉化教程

    注:本文参考了 < confluence搭建破解及汉化教程  > 本文是在yum环境搭建好,且可用联网的前提下进行的实际操作并作记录的. 关于yum本地环境搭建可以参考此文:<Cen ...

  2. linux用户

    hen we are travelling, we find ourselves in new places and new spaces, physically and internally; it ...

  3. 使用Navicat快速生成数据库字典

    https://blog.csdn.net/maquealone/article/details/60764420

  4. vue中 裁剪,预览,上传图片 的插件

    参考地址: https://github.com/dai-siki/vue-image-crop-upload

  5. python(9): GUI

    实例1: 对输入的所有数字求和, 最后以. 结束输入 def fun(): list1=[] print('input a number:') while True: num=input() if n ...

  6. Coding配合git使用时遇到的问题

    转载整理: https://www.jianshu.com/p/b23cd00cffa6 https://www.cnblogs.com/yidoucai/p/5228763.html https:/ ...

  7. 开启Java之旅

    学习应用系统的服务器开发,也许并不算什么“旅行”,也不会那么‘愉快’.但是,我希望这次能够同以往有所不同,更加努力地学习J2EE. 从2月份开始,从事web前端开发,并在公司的的项目中,独立完成了4个 ...

  8. maven如果正常配置不成功,就按照我的就可以配置成功了

    常规的配置如下 非常规的配置 如果上面都没有问题,还是不能创建成功,就是maven配路径的问题,如图所示 如下图所示即是正确配置

  9. 目标检测算法之Faster R-CNN算法详解

    Fast R-CNN存在的问题:选择性搜索,非常耗时. 解决:加入一个提取边缘的神经网络,将候选框的选取交给神经网络. 在Fast R-CNN中引入Region Proposal Network(RP ...

  10. sqlserver数据库不能重命名报错5030

    在学习asp.net的时候使用mssql'经常会出现这种错误,数据库不能重名名5030的错误,其实很简单原因就是有应用程序正在占用这个连接,使用这样一行命令就可以查询出正在占用的连接 use mast ...