1096最小生成树

来源:互联网 发布:java xpath html 编辑:程序博客网 时间:2024/06/06 18:33

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.

 


Output

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


Sample Input

1310 20 300 100 200100 0 300200 300 0


Sample Output

370
[cpp] view plaincopyprint?
  1. #include<iostream>   
  2. #include<stdio.h>   
  3. #include<string>   
  4. using namespace std;  
  5.   
  6. const int maxsize=1000;  
  7. int a[maxsize][maxsize],b[maxsize];  
  8.   
  9. int prim(int n)//prim最小生成数算法   
  10. {  
  11.     int lowcost[maxsize],i,j,k,t,min,sum=0;  
  12.     bool vex[maxsize]={true};//true时表示已被访问,初始化0被访问   
  13.     for(i=0;i<n;i++)  
  14.         lowcost[i]=a[0][i];  
  15.     for(i=1;i<n;i++)  
  16.     {  
  17.         min=10000;  //给一个稍大的数   
  18.         t=0;  
  19.         for(j=1;j<n;j++)  
  20.         {  
  21.             if(lowcost[j]<min&&vex[j]==false&&lowcost[j]!=0)  
  22.                 {  
  23.                     min=lowcost[j];  
  24.                     t=j;  
  25.                 }  
  26.         }  
  27.         sum+=min;  
  28.         k=t;  
  29.         vex[k]=true;  
  30.         for(j=1;j<n;j++)  
  31.         {  
  32.             if(lowcost[j]>a[k][j]&&vex[j]==false&&a[k][j]!=0)  
  33.                 lowcost[j]=a[k][j];  
  34.         }     
  35.     }  
  36.     return sum;  
  37. }  
  38.   
  39. int main()  
  40. {  
  41. //  freopen("e://test.txt","r",stdin);   
  42.     int t,i,j,k,n,val,sum;  
  43.     while(scanf("%d",&t)!=EOF)  
  44.     {  
  45. //      cout<<"t:"<<t<<endl;   
  46.         for(i=0;i<t;i++)  
  47.         {  
  48.             sum=0;  
  49.             memset(a,0,sizeof(a));//这边应该初始为无穷大的,但就这题而言没关系   
  50.             memset(b,0,sizeof(b));  
  51.             scanf("%d",&n);//输入定点个数   
  52. //          cout<<"n:"<<n<<endl;   
  53.             for(j=0;j<n;j++)  
  54.             {  
  55.                 scanf("%d",&val);  
  56.                 b[j]=val;     //存每个适配器的价格   
  57.             }  
  58.             for(j=0;j<n;j++)//输入矩阵   
  59.             {  
  60.                 for(k=0;k<n;k++)  
  61.                 {  
  62.                     scanf("%d",&val);  
  63.                     a[j][k]=val;  
  64.                 }  
  65.             }  
  66.             for(j=0;j<n;j++) //把适配器的价格加入到路径长中   
  67.             {  
  68.                 for(k=0;k<n;k++) //两个点之间为0时代表两者可以直接通过各自的网络适配器进行连接   
  69.                         if(j!=k)    //所以相加后的矩阵是一个完全连通图,问题转化为   
  70.                         {           //求这个完全连通图的各点连接的最短路径,使用Prim算法就可求出   
  71.                             a[j][k]+=b[j]+b[k];  
  72.                         }  
  73.             }  
  74. //          for(j=0;j<n;j++)//输出矩阵   
  75. //          {   
  76. //              for(k=0;k<n;k++)   
  77. //              cout<<a[j][k]<<" ";   
  78. //              cout<<endl;   
  79. //          }   
  80.             sum=prim(n);  
  81.             cout<<sum<<endl;  
  82.         }  
  83.     }  
  84.     return 0;  
  85. }  
[cpp] view plaincopyprint?
  1. int prim(int n)//prim最小生成数算法   
  2. {  
  3.     int lowcost[maxsize],i,j,k,t,min,sum=0;  
  4.     int vex[maxsize];  
  5.     memset(lowcost,maxsize,sizeof(lowcost));  
  6.     memset(vex,0,sizeof(vex));  
  7.     vex[0]=1;  
  8.     for(i=0;i<n;i++)  
  9.         lowcost[i]=a[0][i];  
  10.     for(i=1;i<n;i++)  
  11.     {  
  12.         min=maxsize;  //给一个稍大的数   
  13.         for(j=1;j<n;j++)  
  14.         {  
  15.             if(lowcost[j]<min&&vex[j]!=1&&lowcost[j]!=maxsize)  
  16.                 {  
  17.                     min=lowcost[j];  
  18.                     t=j;  
  19.                 }  
  20.         }  
  21. //      sum+=min;   
  22.         vex[t]=1;  
  23.         for(j=1;j<n;j++)  
  24.         {  
  25.             if(lowcost[j]>a[t][j]&&vex[j]!=1&&a[t][j]!=0)  
  26.                 lowcost[j]=a[t][j];  
  27.         }     
  28.     }  
  29.     for(i=0;i<n;i++) //这样求sum也是可以的   
  30.         sum+=lowcost[i];  
  31.     return sum;  
  32. }  
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大众cc打不着火怎么办 手上扎了仙人掌刺怎么办 pscs5界面字体太小怎么办 儿童做飞机没带户口本怎么办 黑户口想做飞机怎么办 宝宝坐飞机忘记带证件怎么办 值机柜台关闭了怎么办 值机迟到几分钟怎么办 婴儿坐飞机没带证件怎么办 飞机票买了一天降价1000怎么办 社保卡磁性没了怎么办 社保卡民族错了怎么办 坐飞机婴儿出生证明没带怎么办 手提行李超过5kg怎么办 随身行李超过5kg怎么办 南航机票填错身份证怎么办 两岁宝宝坐着驼背怎么办 两岁的宝宝坐晕车怎么办 在机场丢东西了怎么办 孩子放学不按时回家怎么办?转 右腿比左腿粗2cm怎么办 八个月宝宝小腿弯怎么办 南航航班取消了怎么办 高铁不能送老人怎么办 小孩买火车票没有身份证怎么办 断奶后孩子瘦了怎么办 两岁宝宝坐飞机哭闹怎么办 八个月宝宝坐飞机哭闹怎么办 六岁儿童发烧39度怎么办 孩子坐飞机没带证件怎么办 带孩子坐飞机需要什么证件怎么办 婴儿乘飞机没带证件怎么办 吃了轮状发烧怎么办 儿童票比打折票贵怎么办 订机票订错了怎么办 如果飞机不支持婴儿票怎么办 报志愿登不上去怎么办 微单自拍是反的怎么办 蜡笔弄到桌子上怎么办 油画颜料干透了怎么办 数字油画颜料干了怎么办