Sunny Cup 2003 - Preliminary Round

April 20th, 12:00 - 17:00

Problem E: QS Network

In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segment of network cable. Please
be advised that ONE NETWORK ADAPTER CAN ONLY BE USED IN A SINGLE CONNECTION.(ie. if a QS want to setup four connections, it needs to buy four adapters). In the procedure of communication, a QS broadcasts its message to all the QS it is connected with, the
group of QS who receive the message broadcast the message to all the QS they connected with, the procedure repeats until all the QS's have received the message.

A sample is shown below:

A sample QS network, and QS A want to send a message.



Step 1. QS A sends message to QS B and QS C;



Step 2. QS B sends message to QS A ; QS C sends message to QS A and QS D;



Step 3. the procedure terminates because all the QS received the message.

Each QS has its favorate brand of network adapters and always buys the brand in all of its connections. Also the distance between QS vary. Given the price of each QS's favorate brand
of network adapters and the price of cable between each pair of QS, your task is to write a program to determine the minimum cost to setup a QS network.

Input

The 1st line of the input contains an integer t which indicates the number of data sets.



From the second line there are t data sets.



In a single data set,the 1st line contains an interger n which indicates the number of QS.



The 2nd line contains n integers, indicating the price of each QS's favorate network adapter.



In the 3rd line to the n+2th line contain a matrix indicating the price of cable between ecah pair of QS.

Constrains:

all the integers in the input are non-negative and not more than 1000.

Output

for each data set,output the minimum cost in a line. NO extra empty lines needed.

Sample Input

1

3

10 20 30

0 100 200

100 0 300

200 300 0

Sample Output

370

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#define MAXN 1005
#define INF 0x1f1f1f1f using namespace std; using namespace std; int n, ans, cas; //QS适配器的个数,结果,測试数据组数;
int lowcost[MAXN]; //充当Prim算法中的两个数组lowcost和nearvex的作用;
int adapt[MAXN]; //每一个QS喜欢的适配器的价格;
int Edge[MAXN][MAXN]; //邻接矩阵; void Init()
{
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d", &adapt[i]); //输入价格;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++) //邻接矩阵初始化;
{
scanf("%d", &Edge[i][j]);
if(i == j) Edge[i][j] = INF;
else Edge[i][j] += (adapt[i]+adapt[j]);
}
}
memset( lowcost, 0, sizeof(lowcost) );
ans = 0;
} void Prim() //Prim算法核心;
{
lowcost[0] = -1; //从顶点0開始构造最小生成树;
for(int i=1; i<n; i++) lowcost[i] = Edge[0][i];
for(int i=1; i<n; i++) //把其它n-1个顶点扩展到生成树其中;
{
int min = INF, k;
for(int j=0; j<n; j++) //找到当前可用的权值最小的边;
{
if(lowcost[j] != -1 && lowcost[j] < min)
{
k = j;
min = lowcost[j];
}
}
ans += min;
lowcost[k] = -1; //把顶点k扩展进来;
for(int i=0; i<n; i++) if(Edge[k][i] < lowcost[i])
{
lowcost[i] = Edge[k][i];
}
}
printf("%d\n", ans);
} int main()
{
scanf("%d", &cas);
while(cas--)
{
Init();
Prim();
}
return 0;
}

ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;&amp;prim)的更多相关文章

  1. Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告

    Facebook Hacker Cup 2014 Qualification Round比赛Square Detector题的解题报告.单击这里打开题目链接(国内访问需要那个,你懂的). 原题如下: ...

  2. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  3. 最小生成树算法(Prim,Kruskal)

    边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...

  4. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  5. Facebook Hacker Cup 2014 Qualification Round

    2014 Qualification Round Solutions 2013年11月25日下午 1:34 ...最简单的一题又有bug...自以为是真是很厉害! 1. Square Detector ...

  6. 8VC Venture Cup 2016 - Elimination Round D. Jerry's Protest 暴力

    D. Jerry's Protest 题目连接: http://www.codeforces.com/contest/626/problem/D Description Andrew and Jerr ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  8. 8VC Venture Cup 2016 - Elimination Round

    在家补补题   模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, in ...

  9. VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟

    C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

随机推荐

  1. (转载)QT中PRO文件写法的详细介绍,很有用,很重要!

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下: 1. 注释从“#”开始,到 ...

  2. sum(case when then)(男女生的个数)

    判断类似一个班级的男生和女生的人数,用sum (cese when then ) select count(er.execute_result), sum(case er.execute_result ...

  3. Socket开发时,Available为0,实际还有数据的问题

    这段时间处理Socket通讯,比如文件传输,通常代码如下: string filename = @"c:\abc.txt"; // 发送文件名字符串长度(测试代码,实际请传输字符串 ...

  4. Hadoop2.0/YARN深入浅出(Hadoop2.0、Spark、Storm和Tez)

    随着云计算.大数据迅速发展,亟需用hadoop解决大数据量高并发访问的瓶颈.谷歌.淘宝.百度.京东等底层都应用hadoop.越来越多的企 业急需引入hadoop技术人才.由于掌握Hadoop技术的开发 ...

  5. ArcGIS制图——多图层道路压盖处理

    [1]      启动ArcMap,载入各道路要素类.生成名为All-Roads的图层组,如他所看到的,符号级别绘制将对图层组内图层符号起作用: 图层组设置符号等级 [2]      在内容列表中,右 ...

  6. TEXT文本编辑框3 点击按钮添加文本至文本输入框

    In this exercise a function that loads the texts of an internal table into the text window, is imple ...

  7. H2内存数据库 支持存储到文件

     准备工作 1.下载JDK(本人下载的版本号为JDK1.7).环境变量设置JAVA_HOME.设置PATH(%JAVA_HOME%\bin%). 2.下载并解压:h2-2014-07-13.zip ...

  8. Python+Django+SAE系列教程15-----输出非HTML内容(图片/PDF)

    一个Django视图函数 必须 接受一个HttpRequest 实例作为它的第一个參数 返回一个HttpResponse 实例 从一个视图返回一个非HTML 内容的关键是在构造一个 HttpRespo ...

  9. UVA 10795 - A Different Task(递归)

     A Different Task  The (Three peg) Tower of Hanoi problem is a popular one in computer science. Brie ...

  10. uploadfiy使用

    动态加参数:$("#file_upload").uploadify("settings", "formData", { knowledgeI ...