题目:

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. Eclipse ADT 代码注释模版

    具体怎么用: 将下面的内容拷贝出来保存为XML文件,进入,Eclipse :Window --> Java --> Code Style --> Code Templates-> ...

  2. Webapi上传数据(XML)敏感字符解决方案

    方法名加特性 [ValidateInput(false)] 配置文件加 <httpRuntime requestValidationMode="2.0" targetFram ...

  3. Neutron 架构图

    与 OpenStack 其他服务一样,Neutron 采用的是分布式架构,包括 Neutorn Server.各种 plugin/agent.database 和 message queue. Neu ...

  4. 浅谈对MVC的认识

    MVC是model(模型),view(视图),Controller(控制)的缩写. 模型层负责提供数据,和数据库相关的操作都交给模型层处理: 视图层提供交互的界面,其输出数据: 控制层负责接收各种请求 ...

  5. pymongo学习第1篇——增删改查

    参考文档: 1.https://docs.mongodb.org/getting-started/python/ 2.http://api.mongodb.org/python/current/api ...

  6. python相见恨晚的库

    1)基本工具: virtualenv(虚拟环境)pip.setuptools (e.g. easy_install,这些东西肯定要呢)ipython(用了以后,就不再想用普通的python shell ...

  7. CSDN无耻,亿赛通无耻

    吐槽下,自己写一篇关于亿赛通加密文件的简单破解方式,竟然收到请求删除博客的私信,然后那篇博客就没有了. 太过于无耻了.

  8. git问题--Push rejected: Push to origin/master was rejected

    解决git问题 Push rejected: Push to origin/master was rejected 意思是git拒绝合并两个不相干的东西 此时你需要在打开Git Bash,然后进入相应 ...

  9. Xshell 命令后台执行

    但是这样没有在后台启动:因此sh那一行代码需要修改: 前边加上nohup 后边加上& nohup dotnet helloword.dll & 然后,进程启动之后,按任意键进入输入状态 ...

  10. 机器大数据也离不开Hadoop

    转自:http://gtstorageworld.blog.51cto.com/908359/1286758 根据数据来源划分,大数据主要包括三类:商业运作产生的数据.人类行为产生的数据和机器数据.目 ...