Untitled

 avatar
user_2087184
plain_text
7 months ago
4.4 kB
4
Indexable
Never
//Me

#include <iostream>
using namespace std;

int a[101][101], l[101], r[101], n, vert, result;
int vt[101], p[101], parent[101], process[101];
const int temp = 10;

void DFS(int k, int s, int cur, int min) {
	for(int i = 0; i < l[s]; i++) {
		if (k != s && a[s][i] == k && cur > 1) {
			parent[k] = s;
			p[cur] = s;
			p[cur + 1] = k;
			
			for (int j = 0; j <= cur; j++) {
				cout << p[j] << " ";
			}
			cout << k << " " << endl;
			
			int rTemp = r[s] + r[k];
			int minT = rTemp < min ? rTemp : min;
			result += minT;
			//cout << minT <<endl;
			//parent[a[s][i]] = s;
			return;
		}
		if (vt[a[s][i]] != -1 && (vt[a[s][i]] != k + temp || (p[a[s][i]] != s && process[s] != 1))) {
			p[cur] = s;
			vt[a[s][i]] = k + temp;
			int rTemp = r[s] + r[a[s][i]];
			int minT = rTemp < min ? rTemp : min;
			parent[a[s][i]] = s;
			process[a[s][i]] = 1;
			DFS(k, a[s][i], cur + 1, minT);
			process[a[s][i]] = 0;
		}
	}
}

int main() {
	int t;
	cin >> t;
	for (int tc = 1; tc <= t; tc++) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> vert >> r[i] >> l[i];
			for(int j = 0; j < l[i]; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			vt[i] = 0;
			p[i] = 0;
		}
		result = 0;
		for (int i = 0; i < n; i++) {
			vt[i] = -1;
			DFS(i, i, 0, 100000);
		}
		cout << result << endl;
	}
}

//Them
#include<bits/stdc++.h>
using namespace std;

struct canh
{
	int dau, cuoi;
	int trongso;
};
long long ntest, m, i, u, c, v;
long long d[101], dem = 0, dad[101];
canh CA[10001];
long long chiphi = 0;

int FindDad(int u)
{
	if (dad[u] == -1)
	{
		dad[u] = u; return u;
	}
	else return dad[u];
}

void Add(int u, int v)
{
	if (u > v)
		for (int i = 1; i <= m; i++)
		{
			if (dad[i] == u)
				dad[i] = v;
		}
	else if (v > u)
		for (int i = 1; i <= m; i++)
		{
			if (dad[i] == v)
				dad[i] = u;
		}
}
void QUICK_SORT(canh CA[], int left, int right)
{
	if (left == right)
		return;
	if (left < right)
	{
		int k = (left + right) / 2;
		int i = left, j = right;
		while (i <= j)
		{
			while (CA[i].trongso < CA[k].trongso) i++;
			while (CA[j].trongso > CA[k].trongso) j--;
			if (i <= j)
			{
				canh g = CA[i];
				CA[i] = CA[j];
				CA[j] = g;
				i++, j--;
			}
		}
		QUICK_SORT(CA, left, j);
		QUICK_SORT(CA, i, right);
	}
}


void quickSort(canh CA[], int l, int r)
{
	if (l < r) {

		int key = CA[(l + r) / 2].trongso;
		int i = l, j = r;
		while (i <= j)
		{
			while (CA[i].trongso < key) i++;
			while (CA[j].trongso > key) j--;
			if (i <= j)
			{
				if (i < j)
				{
					canh g = CA[i];
					CA[i] = CA[j];
					CA[j] = g;
				}
				i++;
				j--;
			}
		}
		quickSort(CA, l, j);
		quickSort(CA, i, r);
	}
}

void Selec(canh CA[], int n)
{
	for (int i = 1; i <= n - 1; i++)
		for (int j = i + 1; j <= n; j++)
			if (CA[i].trongso > CA[j].trongso)
			{
				canh g = CA[i];
				CA[i] = CA[j];
				CA[j] = g;
			}
}
void process()
{
	//QUICK_SORT(CA,1,dem);
	//Selec(CA,dem);
	quickSort(CA, 1, dem);
	int demcanh = 0, demdinh = 0;
	int i1 = dem, x, y;
	while (demcanh < m - 1 && demdinh < m)
	{
		x = FindDad(CA[i1].dau);
		if (x == -1) demdinh++;
		y = FindDad(CA[i1].cuoi);
		if (y == -1) demdinh++;
		if (x != y)
		{
			Add(x, y); demcanh++;
			chiphi -= CA[i1].trongso;
		}
		//cout <<CA[i1].dau<<" "<<CA[i1].cuoi<<" "<<CA[i1].trongso<<endl;
		i1--;
	}
}
int main()
{
	//freopen("input.txt.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin >> ntest;
	for (int j = 1; j <= ntest; j++)
	{
		cin >> m; chiphi = 0, dem = 0;
		for (int jj = 1; jj <= m; jj++)
		{
			cin >> i >> u >> c;
			d[i] = u, dad[i] = -1;
			for (int j1 = 1; j1 <= c; j1++)
			{
				cin >> v;
				if (i > v)
				{
					CA[++dem].dau = i; CA[dem].cuoi = v;
					CA[dem].trongso = d[i] + d[v];
					chiphi += CA[dem].trongso;
				}
			}
		}
		process();
		//for(int i=1;i<=dem;i++)
		//	cout <<CA[i].dau<<"  "<<CA[i].cuoi<<"  "<<CA[i].trongso<<endl;
		cout << chiphi << endl;

	}
}

// Case

5
11
0 1 2
1 10
1 1 4
0 2 4 6
2 3 2
1 10
3 1 2
7 8
4 1 2
1 5
5 6 2
4 6
6 5 2
1 5
7 1 2
3 9
8 4 2
3 9
9 3 2
7 8
10 2 2
0 2
3
0 1 2
1 2
1 2 2
0 2
2 3 2
0 1
4
0 1 2
1 2
1 8 2
0 3
2 16 2
0 3
3 12 2
1 2

5
6
0 1 3
1 3 5
1 2 2
0 2
2 3 2
1 3
3 4 3
0 2 4
4 5 2
3 5
5 6 2
0 4

5
13
0 1 3
1 10 11
1 1 4
0 2 4 6
2 3 3
1 10 12
3 1 2
7 8
4 1 3
1 5 11
5 6 2
4 6
6 5 3
1 5 12
7 1 2
3 9
8 4 2
3 9
9 3 2
7 8
10 2 2
0 2
11 6 2
0 4
12 1 2
2 6

5
9
0 1 2
1 3
1 2 3
0 2 4
2 3 2
1 5
3 4 3
0 4 6
4 5 4
1 3 5 7
5 6 3
2 4 8
6 7 2
3 7
7 8 3
4 6 8
8 9 2
5 7
Leave a Comment