天天看點

ZOJ 1586 QS Network (經典MST~Prim)

連結:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemid=586

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:

ZOJ 1586 QS Network (經典MST~Prim)

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

分析:

在構造有向網時,每條邊的權值為兩個qs的擴充卡價格加上這兩個qs之間的網線的價格,而且不需要記錄構造最小生成樹時選擇的邊;

代碼:

繼續閱讀