题目:

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

题意描述:
题目描述的很有意思,不过有点绕。简单来说就是每两个人要建立联系,需要分别购买一个自己最喜欢的接收器,和共同购买一个共用转换器。

先输入结点的个数N,再输入N个数,表示结点最喜欢的接收器的价格,最后输入N*N的矩阵表示i和j建立联系的转换器价格。

解题思路:

该开始的时候想用Prim算法,在选择一条边可以建造的时候,将两个结点都计一下数,最后乘以每个结点喜欢的接收器的价格再加上最小生成树的值。但是这种解法是有漏洞的,因为如果接收器是后来加的,它的费用是没有考虑进最省路径的,所以有可能,接收器的价格很贵,虽然转换器是最省的但加上接收器的话就不是最省路径了。综上所述,选择Kruskal算法,不同的是将接收器的价格事先计算在这条边上,那么就跟模板题一样了。

代码实现:

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct edge
{
int u,v,w;
};
int cmp(struct edge x,struct edge y)
{
return x.w<y.w;
}
struct edge e[*];
int merge(int v,int u);
int getf(int v);
int map[][],f[];
int main()
{
int t,n,a[],i,j,k,c,sum;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]); k=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
e[k].u=i;
e[k].v=j;
e[k++].w=map[i][j]+a[i]+a[j];
}
}
sort(e+,e+k,cmp);
for(i=;i<=n;i++)
f[i]=i;
sum=;
c=;
for(i=;i<k;i++)//边的数目
{
if( merge(e[i].u,e[i].v) )
{
c++;
sum += e[i].w;
}
if( c == n-)
break;
}
printf("%d\n",sum);
}
return ;
} int merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1 != t2)
{
f[t2]=t1;
return ;
}
return ;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}

易错分析:

1、注意遍历的时候遍历的是边的条数(代码功力还是要加强的)

ZOJ 1586 QS Network(Kruskal算法求解MST)的更多相关文章

  1. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

  2. ZOJ - 1586 QS Network (Prim)

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

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

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

  4. POJ 1251 Jungle Roads(Kruskal算法求解MST)

    题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...

  5. HDU 2682 Tree(Kruskal算法求解MST)

    题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...

  6. HDU 5253 连接的管道(Kruskal算法求解MST)

    题目: 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了.当老 J ...

  7. ZOJ 1586 QS Network MST prim水题

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

  8. zoj 1586 QS Network

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

  9. ZOJ 1203 Swordfish(Prim算法求解MST)

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

随机推荐

  1. LoadRunner 12下载和安装教程

    我们利用LoadRunner可以对Web应用系统进行性能压力测试,本篇博客将和大家介绍下LoadRunner 12的下载和安装,在后续的博客中将和大家介绍其使用的方法. 1.LoadRunner 12 ...

  2. cmd命令使用笔记

    使用资源管理器打开当前路径 explorer %cd%

  3. SQL Server2008 R2 数据库镜像实施手册(双机)

    一.配置主备机 1. 服务器基本信息 主机名称为:HOST_A,IP地址为:192.168.1.155 备机名称为:HOST_B,IP地址为:192.168.1.156 二.主备实例互通 实现互通可以 ...

  4. 前端工具Rythem介绍

    Rythem是一个与Fiddler同类的软件,和Fiddler一样具有 代理抓包/替换 功能,与Fiddler最大的不同是Rythem是跨平台&开源的. 另外,根据笔者的一次开发经验,Ryth ...

  5. 940. Distinct Subsequences II

    Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...

  6. git 常用命令(不定期更新)

    过程写写吧,总是忘记.1,在一个文件夹下 键入 git init ,使之成为Git可以管理的仓库.2,编写一个文件readme.txt.3,把文件添加到仓库 git add readme.txt4,把 ...

  7. java的几种定时任务

    本篇博文主要是讲述2.x 版本的quartz下的实现方案,1.x 版本的实现方式大致原理一致,但是具体的实现方式有些不一致,具体体现在获取  scheduler 这个类的方式上有些不同,这里不作过多的 ...

  8. 解决 ArchLinux 下中文 Chinese 不能输入 couldnt input 的问题

    解决 ArchLinux 下中文 Chinese 不能输入 couldnt input 的问题 一.Question 一年多的 ArchLinux 用户再次回归.然鹅,见面礼就是终端不能输入中文. 在 ...

  9. 《JAVA与模式》之桥梁模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述桥梁(Bridge)模式的: 桥梁模式是对象的结构模式.又称为柄体(Handle and Body)模式或接口(Interface)模式. ...

  10. CentOS7搭建FastDFS V5.11分布式文件系统-第三篇

    1.测试 前面两篇博文已对FastDFS的安装和配置,做了比较详细的讲解.FastDFS的基础模块都搭好了,现在开始测试下载. 1.1 配置客户端 同样的,需要修改客户端的配置文件: /etc/fdf ...