题目大意:

给出的案例结果得出步骤,如下图所示,从结点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. flask 中orm关系映射 sqlalchemy的查询

    flask的orm框架(SQLAlchemy)-一对多查询以及多对多查询   一对多,多对多是什么? 一对多.例如,班级与学生,一个班级对应多个学生,或者多个学生对应一个班级. 多对多.例如,学生与课 ...

  2. Spark-SQL之DataFrame操作

    Spark SQL中的DataFrame类似于一张关系型数据表.在关系型数据库中对单表或进行的查询操作,在DataFrame中都可以通过调用其API接口来实现.可以参考,Scala提供的DataFra ...

  3. PHP 命名空间与自动加载机制

    include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...

  4. Best Cow Line(POJ3617)

    Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year&quo ...

  5. Centos7上vsftp脚本--> sh vsftp.sh 用户名 密码 --> sh vsftp.sh install

    #!/bin/bash #vsftp install . /etc/rc.d/init.d/functions users=/etc/vsftpd/vftpuser.txt login=/etc/vs ...

  6. php模拟数据请求

    php:模拟后台接受数据的步骤<?php> 1.连接数据库 $host="localhost"; $uname="root"; $upwd=&quo ...

  7. js里添加的标签

    js里添加的标签.网页加载此标签绑定的js函数时,由于没有标签,故无法执行函数. 例如: js中添加了一个button: html1 += "<td><button typ ...

  8. Python 9*9口诀

    #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:Liuyoushui # Time = 2017/7/18 10:33 print ('\n ...

  9. python requests 正则爬虫

    代码: import requests from multiprocessing import Pool from requests.exceptions import RequestExceptio ...

  10. SQL Server索引维护

    索引维护的两个重要方面是索引碎片和统计信息. 一:索引碎片 降低碎片的产生,当索引上的页不在具有物理连续性时,就会产生碎片,下面的情景会产生碎片: INSERT操作.UPDATE操作.DBCC SHR ...