/*
zoj_1586 最小生成树
简单变形,水过
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
struct way
{
int from,to;
int value;
};
way w[1000010];
int price[1010];
priority_queue <way>pq;
int father[1010];
int sum;
bool operator<( const way&a,const way&b )
{
return a.value>b.value;
}
int find_set( int a )
{
if( father[a]!=a )
{
father[a]=find_set( father[a] );
}
return father[a];
}
void union_set( int a,int b,int c )
{
if( a==b ) return;
father[b]=a;
sum+=c;
}
int main()
{
int T,n,i,j,t;
way temp;
scanf( "%d",&T );
while( T-- )
{
scanf("%d",&n);
for( i=0;i<n;i++ )
{
scanf( "%d",&price[i] );
father[i]=i;
}
for( i=0;i<n;i++ )
{
for( j=0;j<n;j++ )
{
scanf( "%d",&t );
if( j<=i ) continue;
temp.from=i;
temp.to=j;
temp.value=t+price[i]+price[j];
pq.push(temp);
}
}
sum=0;
while( !pq.empty() )
{
temp=pq.top(); pq.pop();
union_set( find_set(temp.from),find_set(temp.to),temp.value );
}
printf( "%d\n",sum );
}
return 0;
}
zoj 1586
最新推荐文章于 2022-08-02 12:08:03 发布