QS Network

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.

<b< dd="">

Output

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

<b< dd="">

Sample Input

1
3
10 20 30
0 100 200
100 0 300
200 300 0

<b< dd="">

Sample Output

370

题意:给你一些含权值的点,以矩阵形式给你一些含权边(总权值为边权+两端点权),让你找出连接所有端点的最小生成树。

思路:kruskal。每次找当前最小边,若这条边和已有的边不构成环(并查集判断),则将其加入生成树。

#include<stdio.h>
#include<algorithm>
using namespace std; int f[],a[];
struct Edge{
int u,v,w;
}edge[]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} int kru(int c,int n)
{
int i;
for(i=;i<=n;i++){
f[i]=i;
}
sort(edge+,edge+c+,cmp);
int cnt=,ans=;
for(i=;i<=c;i++){
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=w;
f[fv]=fu;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
}
int main()
{
int t,n,x,c,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
c=;
for(i=;i<=n;i++){
for(j=;j<=n;j++){
scanf("%d",&x);
if(i<j){
edge[++c].u=i;
edge[c].v=j;
edge[c].w=x+a[i]+a[j];
}
}
}
printf("%d\n",kru(c,n));
}
return ;
}

ZOJ 1586 QS Network Kruskal求最小生成树的更多相关文章

  1. ZOJ - 1586 QS Network (Prim)

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

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

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

  3. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

  4. ZOJ 1586 QS Network MST prim水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...

  5. zoj 1586 QS Network

    最小生成树,刚刚学了Prim算法. 对每条边变的权值进行预处理,c[i][j] = c[i][j] + p[i] + p[j] 其中c[i][j]为输入的权值,p[i],p[j]为连接这两个节点所需的 ...

  6. Kruskal求最小生成树

    #include<bits/stdc++.h> using namespace std; ; ; const int inf = 0x3f3f3f3f; ; typedef long lo ...

  7. POJ 1861 Network (Kruskal求MST模板题)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Specia ...

  8. Codeforces 609E (Kruskal求最小生成树+树上倍增求LCA)

    题面 传送门 题目大意: 给定一个无向连通带权图G,对于每条边(u,v,w)" role="presentation" style="position: rel ...

  9. Prim和Kruskal求最小生成树

    Prim: 算法步骤: 1.任意结点开始(不妨设为v1)构造最小生成树: 2.首先把这个结点(出发点)包括进生成树里, 3.然后在那些其一个端点已在生成树里.另一端点还未在生成树里的所有边中找出权最小 ...

随机推荐

  1. C#Panel 控件的使用

    Windows 窗体 Panel 控件用于为其他控件提供可识别的分组.通常,使用面板按功能细分窗体.例如,可能有一个订单窗体,它指定邮寄选项(如使用哪一类通营承运商).将所有选项分组在一个面板中可向用 ...

  2. 我的Android进阶之旅------>解决Your project contains error(s),please fix them

    在使用eclipse写好Android的代码,代码没有报错.然后想在AVD中运行测试时,弹出错误框,提示信息为:  "Your project contains error(s),pleas ...

  3. python 函数中的递归、lambda 、map reduce 等详解

    举例说明 #例1: ###递归函数求和 from traitlets.traitlets import Instance def mysum(L): print(L) if not L: return ...

  4. MySQL——sql语句的执行顺序

    #核心知识点: 书写顺序:where——>group by——>having——>order by——>limit 许多时候在书写一些复杂的sql语句的时候,尤其是在渐进式推导 ...

  5. Java多线程系列 基础篇06 synchronized(同步锁)

    转载 http://www.cnblogs.com/paddix/ 作者:liuxiaopeng http://www.infoq.com/cn/articles/java-se-16-synchro ...

  6. 51Nod 1294 修改数组 —— LIS

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1294 1294 修改数组  题目来源: HackerRank ...

  7. openstack制作镜像官网地址

    http://docs.ocselected.org/openstack-manuals/kilo/image-guide/content/ch_creating_images_automatical ...

  8. deepin 安装微信与QQ

    安装QQ sudo apt-get install deepin.com.qq.im 安装微信 sudo apt-get install deepin.com.wechat 附录 其他安装包 http ...

  9. Hadoop- MapReduce在实际应用中常见的调优

    1.Reduce Task Number 通常来说一个block就对应一个map任务进行处理,reduce任务如果人工不去设置干预的话就一个reduce.reduce任务的个数可以通过在程序中设置   ...

  10. 鸟哥的linux私房菜 - 第三章 主机规划与磁盘分区

    各硬件装置在linux中的文件名 在linux系统中,每个装置都被当成一个档案来对待. 常见的装置与其在linux中的档名: 磁盘分区 磁盘链接的方式与装置文件名的关系 个人计算机常见的磁盘接口有两种 ...