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. Spring MVC程序

    Spring MVC程序(IDEA开发环境)   回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的 ...

  2. css3 animation 参数详解

    animation: name 2s ease 0s 1 both有人知道这后面的参数都代表什么意思吗 name 就是你创建动画的名称 2S表示的时长 ease表示运动效果 0S表示延迟时间 1表示的 ...

  3. DRP——Servlet(一)

    Servlet Servlet是用java语言编写的程序,执行在Webserver上,用来处理来自client的请求:通常会把处理的结果以HTML的形式返回,在client形成动态网页.事实上能够理解 ...

  4. 远光软件ASP.NET笔试题小汇总

    ASP.NET笔试题是ASP.NET程序员面试必须经历的,一般会叫你填两个表 1个是你的详细信息表 1个是面试题答卷 两个都要注意反正面是否都有内容不要遗漏,如果考你机试一般也有两种,就是程序连接数据 ...

  5. ASP.NET、HTML+CSS - 弹出提示窗体

    刷新数据,提示之后,CSS样式改变: 解决方案: 在ASP.NET中,如果是添加信息成功之后出现提示信息,那么只能用  ClientScript.RegisterStartupScript(this. ...

  6. 刚写好的读取多网卡IP地址的函数

    虽然现在一机多网卡已经很普遍(像Notebook带有线.无线.蓝芽等),但是找一个现成的能够一次过读出所有网卡IP地址的函数实在是难,无奈自己写了一个,好东西谁用谁知道. //uses WinSock ...

  7. /etc/security/limits.conf 设置

    jrhdpt01:/root# cat /etc/security/limits.conf  * soft nofile 65535  * hard nofile 65535  * soft npro ...

  8. linux公社的大了免费在线android资料

    2011年linux数据库的android在线分享 linux公社:开源公社             本文撰写:杨凯专属频道 2011年9月12日 21:39 <目录> Android 3 ...

  9. 根据图像路径,创建CBitmap对象的方法

    因为项目的关系,需要根据图像路径,创建CBitmap对象.起初查资料找到了LoadBitmap这个函数,根据CSDN得 BOOL LoadBitmap ( LPCTSTR lpszResourceNa ...

  10. 让MFC程序隐藏运行界面

    在MFC中隐藏运行界面确实花花点功力. 针对对话框程序,一种不是很好地实现方法是在OnPaint函数中添加如下代码: CWnd::ShowWindow(SW_HIDE); 添加后执行会发现屏幕会闪烁一 ...