1096最小生成树

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

1
3
10 20 30
0 100 200
100 0 300
200 300 0


Sample Output

370
  
  
  
  
[cpp] view plain copy print ?
  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. }  
#include<iostream> #include<stdio.h> #include<string> using namespace std; const int maxsize=1000; int a[maxsize][maxsize],b[maxsize]; int prim(int n)//prim最小生成数算法 { int lowcost[maxsize],i,j,k,t,min,sum=0; bool vex[maxsize]={true};//true时表示已被访问,初始化0被访问 for(i=0;i<n;i++) lowcost[i]=a[0][i]; for(i=1;i<n;i++) { min=10000; //给一个稍大的数 t=0; for(j=1;j<n;j++) { if(lowcost[j]<min&&vex[j]==false&&lowcost[j]!=0) { min=lowcost[j]; t=j; } } sum+=min; k=t; vex[k]=true; for(j=1;j<n;j++) { if(lowcost[j]>a[k][j]&&vex[j]==false&&a[k][j]!=0) lowcost[j]=a[k][j]; } } return sum; } int main() { // freopen("e://test.txt","r",stdin); int t,i,j,k,n,val,sum; while(scanf("%d",&t)!=EOF) { // cout<<"t:"<<t<<endl; for(i=0;i<t;i++) { sum=0; memset(a,0,sizeof(a));//这边应该初始为无穷大的,但就这题而言没关系 memset(b,0,sizeof(b)); scanf("%d",&n);//输入定点个数 // cout<<"n:"<<n<<endl; for(j=0;j<n;j++) { scanf("%d",&val); b[j]=val; //存每个适配器的价格 } for(j=0;j<n;j++)//输入矩阵 { for(k=0;k<n;k++) { scanf("%d",&val); a[j][k]=val; } } for(j=0;j<n;j++) //把适配器的价格加入到路径长中 { for(k=0;k<n;k++) //两个点之间为0时代表两者可以直接通过各自的网络适配器进行连接 if(j!=k) //所以相加后的矩阵是一个完全连通图,问题转化为 { //求这个完全连通图的各点连接的最短路径,使用Prim算法就可求出 a[j][k]+=b[j]+b[k]; } } // for(j=0;j<n;j++)//输出矩阵 // { // for(k=0;k<n;k++) // cout<<a[j][k]<<" "; // cout<<endl; // } sum=prim(n); cout<<sum<<endl; } } return 0; }
  
  
  
  
[cpp] view plain copy print ?
  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. }  

你可能感兴趣的:(1096最小生成树)