您的位置:首页 > 其它

1096 prim 最小生成数

2009-08-07 09:54 295 查看
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

#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;
}
[/code]
int prim(int n)//prim最小生成数算法
{
int lowcost[maxsize],i,j,k,t,min,sum=0;
int vex[maxsize];
memset(lowcost,maxsize,sizeof(lowcost));
memset(vex,0,sizeof(vex));
vex[0]=1;
for(i=0;i<n;i++)
lowcost[i]=a[0][i];
for(i=1;i<n;i++)
{
min=maxsize;  //给一个稍大的数
for(j=1;j<n;j++)
{
if(lowcost[j]<min&&vex[j]!=1&&lowcost[j]!=maxsize)
{
min=lowcost[j];
t=j;
}
}
//		sum+=min;
vex[t]=1;
for(j=1;j<n;j++)
{
if(lowcost[j]>a[t][j]&&vex[j]!=1&&a[t][j]!=0)
lowcost[j]=a[t][j];
}
}
for(i=0;i<n;i++) //这样求sum也是可以的
sum+=lowcost[i];
return sum;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: