QS Network


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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


题目的意思是给出n个点,每个点有点权,现在要将所有点连通,连接两个点的花费是2个点的点权加这条边的边权,问最小花费
思路:奖点权并到相应的边上,然后做最小生成树


#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
#define LL long long struct node
{
int u,v,w;
} p[1000005];
int n,cnt,pre[1005];
int mp[1005][1005],a[1005];
bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<1005; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} void kruskal()
{
sort(p,p+cnt,cmp);
init();
int cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==n-1)
{
break;
}
}
printf("%d\n",cost);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&mp[i][j]);
cnt=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
p[cnt].u=i,p[cnt].v=j;
p[cnt++].w=mp[i][j]+a[i]+a[j];
}
kruskal();
}
return 0;
}



ZOJ1586 QS Network 2017-04-13 11:46 39人阅读 评论(0) 收藏的更多相关文章

  1. Squares 分类: POJ 2015-08-04 11:46 3人阅读 评论(0) 收藏

    Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17462 Accepted: 6634 Description ...

  2. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  3. 修改Linux中的用户名 分类: B3_LINUX 2014-07-24 11:40 440人阅读 评论(0) 收藏

    需要修改2个文件: /etc/hosts /etc/sysconfig/network 然后重启 1.修改/etc/sysconfig/network NETWORKING=yes HOSTNAME= ...

  4. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  5. Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏

    哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  6. 企业证书APP发布流程 分类: ios相关 app相关 2015-06-10 11:01 212人阅读 评论(0) 收藏

    企业发布app的 过程比app store 发布的简单多了,没那么多的要求,哈 但是整个工程的要求还是一样,比如各种像素的icon啊 命名规范啊等等. 下面是具体的流程 1.修改你的 bundle i ...

  7. Java解析HTML之HTMLParser使用与详解 分类: C_OHTERS 2014-05-19 21:46 2309人阅读 评论(0) 收藏

    转自:http://free0007.iteye.com/blog/1131163 HTMLParser具有小巧,快速的优点,缺点是相关文档比较少(英文的也少),很多功能需要自己摸索.对于初学者还是要 ...

  8. 用IBM WebSphere DataStage进行数据整合: 第 1 部分 分类: H2_ORACLE 2013-08-23 11:20 688人阅读 评论(0) 收藏

    转自:http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0602zhoudp/ 引言 传统的数据整合方式需要大量的手工 ...

  9. Curling 2.0 分类: 搜索 2015-08-09 11:14 3人阅读 评论(0) 收藏

    Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14289 Accepted: 5962 Descript ...

随机推荐

  1. 设置cassandra用户名和密码

    参考http://zhaoyanblog.com/archives/307.html 修改cassandra.yaml配置文件 把默认的 authenticator: AllowAllAuthenti ...

  2. hudson插件说明

    Artifactory Plugin:maven仓库管理工具 Backup plugin 可以备份hudson_home下所有文件,除了svncode.这个插件有问题,不能使用. Build Publ ...

  3. 汇编_指令_SUB

    SUB是减法运算. 比如mov ax,2mov bx,1sub ax,bx 其中sub ax,bx就是ax中的值减bx中的值,等于1,然后把结果,也就是1,放入ax中.

  4. C++ 函数特性_函数重载

    函数重载 在相同作用域内 用同一函数名定义的多个函数: 参数个数和参数类型不同 int getMax(int x,int y,int z) // 函数名相同都是 getMax() { // to do ...

  5. pyinstaller的使用方法 by 王大龙

    ---------------------------------------------------------------------------------------------------- ...

  6. python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库

    前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 “test-mysql”. Python 操作 MySQL 我们使用 ...

  7. MySQL 通用查询日志(General Query Log)

      同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志可以帮助我们定位mysql ...

  8. 2_bootstrap的环境搭建

    2.bootstrap环境搭建 2.1.下载资源 中文官网地址:http://d.bootcss.com/bootstrap-3.3.5.zip http://www.bootcss.com 2.2. ...

  9. 使用Fiddler对IPhone手机的应用数据进行抓包分析

    原文出自: http://www.cr173.com/html/20064_1.html Fiddler能捕获ISO设备发出的请求,比如IPhone, IPad, MacBook. 等等苹果的设备.  ...

  10. Centos 7.2基础安装和配置(含分区方案建议)

    景:windows桌面运维为主的我,前几天接到一个去某客户上架安装服务器的工作任务,含糊的说要上架几台服务器顺便安装Centos系统,于是我便下载了一个Centos7.2版本的镜像,顺利的用USBwr ...