Untitled

 avatar
unknown
plain_text
2 years ago
1.6 kB
1
Indexable
#include<iostream>
#define max 105
using namespace std;
int n;          // so thanh
int a[max][max]; // luu ma tran ke
int visit[max];
int maybd[max];
int ans;
bool flag;
int const vc = 1000000;

void reset(){
	for(int i=0; i <n ; i++){
		visit[i] =0;
	}
}

void dfs(int tempthanh,int thanh, int sl, int tempMin, int d1, int d2){
	if(tempthanh == thanh && sl >=3){
		flag =true;
		ans += tempMin;
		a[d1][d2] =0;
		a[d2][d1] =0;
		return;
	}
	for(int col =0; col < n; col++){
		if(flag) return;
		if(a[tempthanh][col] == 1 && (!visit[col] || (col == thanh && sl >=3))){
			visit[col] =1;
			int value = maybd[tempthanh] + maybd[col];
			if(tempMin > value){
				dfs(col, thanh, sl+1, value, tempthanh, col);
			}
			else {
				dfs(col, thanh, sl+1,tempMin, d1, d2);

			}
		}
	}
}



void solve(){
	for(int thanh=0; thanh <n ; thanh++){ //duyet cac thanh de dfs
		reset();
		visit[thanh]=1;
		flag = false;
		dfs(thanh, thanh, 1, vc, -1, -1);
		while(flag){
			reset();
			visit[thanh]=1;
			flag = false;
			dfs(thanh, thanh, 1, vc, -1, -1);
		}
	}
}



int main(){
	freopen("Text.txt", "r", stdin);
	int test;
	cin >> test;
	for(int tc =1; tc<=test; tc++){
		cin >>n;
		for(int i=0; i <n ; i++){
			for(int j=0; j <n ; j++){
				a[i][j] =0;
			}
		}
		int thanh, sl_maybd, sl_ke,tempthanh;
		for(int i=0; i <n ; i++){
			cin >> thanh >> sl_maybd >> sl_ke;
			maybd[thanh] = sl_maybd;
			for(int k=0; k <sl_ke ; k++){
				cin >> tempthanh;
				a[thanh][tempthanh]=1;
			}
		}
		ans=0;
		solve();
		cout << ans <<endl;

	}		
	return 0;
}