Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
3
Indexable
Never
#include<iostream>
using namespace std;

int SL[100];
int map[100][101];
int check[1001];
int sum=0;
int key[1001];
int maxKey(int N){
	int max =-1;
	int index =-1;
	for(int i=0;i<N;i++){
		if(check[i] == 0 && max < key[i]){
			max = key[i];
			index =i;
		}
	}
	return index;
}
void search(int x, int N){
	key[x] =0;
	for(int i=0;i<N-1;i++){
		int u = maxKey(N);
		if(u == -1) return;
		check[u]=1;
		for(int v=0;v<N;v++){
			if(check[v] == 0 /*&& map[u][v] >= 0*/ && map[u][v] > key[v]){
				key[v] = map[u][v];
			}
		}
	}
}
void reset(int N){
	for(int i=0;i<N;i++){
		key[i] =-1;
		check[i] =0;
		SL[i]=0;
		for(int j=0;j<N;j++){
			map[i][j] =0;
		}
	}
}
int main(){
	//freopen("Text.txt","r",stdin);
	int count;
	cin>>count;
	for (int l1 = 1; l1 <= count; l1++){
		int N;
		cin>>N;
		reset(N);
		//nhap input-------------------------------
		for(int i=0;i<N;i++){
			int a,b;
			cin>>a>>b;
			SL[a] = b;
			int c;
			cin>>c;
			for(int j=0;j<c;j++){
				int d;
				cin>>d;
				map[a][d] =1;
				map[d][a] =1;
			}
		}
		//-----------------------------------------
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(i!=j && map[i][j] == 1){
					map[i][j] = SL[i]+SL[j];
				}
			}
		}

		for(int i=0;i<N-1;i++){
			if(check[i] == 0 && key[i] == -1){
				search(i,N);
			}
		}
		int ans=0;
		for(int i=0;i<N;i++){
			for(int j=i+1;j<N;j++){
				ans+=map[i][j];
			}
		}
		for(int i=0;i<N;i++){
			if(key[i] >0 ) ans-= key[i];
		}
		cout<<ans<<endl;
	}

	return 0;
}