思路:题意杀...能看懂题目就很好做了,边的权值还要加上两个点的权值,然后就是模板了
Trick:边的数量很多,数组要开大一点...开小了居然不是RE..而是WA...
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 1100
#define LL long long
int cas=1,T;
const int maxm = 1000010;
struct Edge
{
int u,v;
int dist;
Edge(){}
Edge(int u,int v,int d):u(u),v(v),dist(d){}
bool operator < (const Edge&rhs)const
{
return dist < rhs.dist;
}
};
int n,m;
Edge edges[maxm];
int pre[maxn];
int Find(int x)
{
return pre[x]==-1?x:Find(pre[x]);
}
void init()
{
m=0;
memset(pre,-1,sizeof(pre));
}
void AddEdge(int u,int v,double dist)
{
edges[m++]=Edge(u,v,dist);
}
LL Kruskal()
{
LL sum = 0;
int cnt = 0;
sort(edges,edges+m);
for (int i = 0;i<m;i++)
{
int u = edges[i].u;
int v = edges[i].v;
if (Find(u) != Find(v))
{
pre[Find(u)] = Find(v);
sum+=edges[i].dist;
if (++cnt >= n-1)
break;
}
}
return sum;
}
int a[maxn];
int main()
{
//freopen("in","r",stdin);
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
init();
for (int i = 0;i<n;i++)
scanf("%d",&a[i]);
for (int i = 0;i<n;i++)
for (int j = 0;j<n;j++)
{
int temp;
scanf("%d",&temp);
if (i<j)
AddEdge(i,j,temp+a[i]+a[j]);
}
printf("%lld\n",Kruskal());
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}