显示代码纯文本
#include<cstdio>
#include<algorithm>
using namespace std;
int ufs[10000];
int find(int x){
if(ufs[x]!=x)ufs[x]=find(ufs[x]);
return ufs[x];
}
void link(int a,int b){
ufs[find(a)]=find(b);
}
struct edge{
int a,b,cost;
}lst[100000];
int len = 0;
bool cmp(const edge &a,const edge &b){
return a.cost<b.cost;
}
bool install[10000][2][11];
int fuwu[15],kehu[15];
int main(){
freopen("net.in","r",stdin);
freopen("net.out","w",stdout);
int n;scanf("%d",&n);
for(int i = 1;i<=n;++i)ufs[i]=i;
int x;char tmp;
for(int i = 1;i<=n;++i){
while(1){
scanf("%d",&x);if(x==0)break;
install[i][0][x]=true;
tmp=getchar();
if(tmp=='\n')break;
}
while(1){
scanf("%d",&x);if(x==0)break;
install[i][1][x]=true;
tmp=getchar();
if(tmp=='\n')break;
}
}
for(int i = 1;;++i){
scanf("%d",&x);
fuwu[i]=x;
tmp=getchar();
if(tmp=='\n')break;
}
for(int i = 1;;++i){
scanf("%d",&x);
kehu[i]=x;
tmp=getchar();
if(tmp=='\n'||tmp==EOF)break;
}
for(int i=1;i<=n;++i){
for(int j=1;j<=10;++j){
for(int k=1;k<=n;++k){
if(install[i][0][j]&&install[k][1][j]){
lst[len].a=i;lst[len].b=k;
lst[len].cost=fuwu[j]+kehu[j];
len++;
}
}
}
}
sort(lst,lst+len,cmp);
int linkv = 1,ans=0;
for(int i = 0;i<len;++i){
if(find(lst[i].a)!=find(lst[i].b)){
link(lst[i].a,lst[i].b);
linkv++;
ans+=lst[i].cost;
if(linkv==n)break;
}
}
printf("%d",ans);
fclose(stdin);fclose(stdout);
return 0;
}