zoj 1586 QS Network

来源:互联网 发布:电子文件的元数据 编辑:程序博客网 时间:2024/05/29 17:57

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



题意概括:在一个行星上有几个生物需要联系,每两个人需要联系都需要转换器和电缆,一个电缆上只能插一根电缆,如果想要和两个人联系则需要买两个转换器。

解题思路:处理数据的时候直接把两个人需要的转换器加到对应的电缆费用上。

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define M 1100int i,j,m,n,l;int f[M];int b[M][M],a[M];struct edge{int u,v,w;}e[M*M];bool cmp(edge a,edge b){return a.w<b.w;}int getf(int u){if(f[u]==u){return u;}else{f[u]=getf(f[u]);return f[u];}}int merge(int u,int v){int t1=getf(u);int t2=getf(v);if(t1!=t2){f[t2]=t1;return 1;}return 0;}void Kruskal(){int num=0,sum=0;for(i=1;i<=n;i++){f[i]=i;}for(i=0;i<l;i++){if(merge(e[i].u,e[i].v)!=0){num++;sum+=e[i].w;}if(num-1==n)break;}printf("%d\n",sum);}int main(){int t;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i]);}for(i=1;i<=n;i++){for(j=1;j<=n;j++){scanf("%d",&b[i][j]);}}l=0;for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){e[l].u=i;e[l].v=j;e[l].w=b[i][j]+a[i]+a[j];l++;}}/*for(i=0;i<l;i++){//printf("%d %d %d \n",e[i].u,e[i].v,e[i].w);}*/sort(e,e+l,cmp);Kruskal();}return 0;}
错误分析:

开始是先算出花费最少电缆费用即可将全部生物连接的费用然后加上需要转换器的费用,但是如果转换器很贵的话,先算出最少电缆费用在加上转换器费用未必是最省的,所有需要把转换器费用提前加到电缆费用上算然后按照总费用计算最省的值。

原创粉丝点击