在D题的基础之上,加上了适配器的应用,在边的权值之上加上了两个适配器的权值罢了
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=1000+10;
int par[maxn],a[maxn];
struct node{
int u,v,len;
friend bool operator <(node a,node b)
{
return a.len>b.len;
}
};
int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
}
int main()
{
int t,r;
//freopen("E.txt","r",stdin);
scanf("%d",&r);
while(r--)
{
scanf("%d",&t);
for(int i=1;i<=t;i++)
scanf("%d",&a[i]);
priority_queue<node> pq;
for(int i=1;i<=t;i++)
{
node s;s.u=i;
par[s.u]=i;
for(int j=1;j<=t;j++)
{
s.v=j;
scanf("%d",&s.len);
int adapter=a[i]+a[j];
s.len+=adapter;
//cout<<s.len<<endl;
pq.push(s);
}
}
int ans=0;
node th;
while(!pq.empty())
{
th=pq.top();pq.pop();
int u=find(th.u),v=find(th.v);
if(u!=v)
{
par[u]=v;
ans+=th.len;
}
}
printf("%d\n",ans);
}
return 0;
}