1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
| #include <bits/stdc++.h>
using namespace std;
#define USE_CPPIO() ios_base::sync_with_stdio(0); cin.tie(0)
#define INF 0x3f3f3f3f
#define NINF 0xc0c0c0c0
struct point {
int x, y;
};
struct buy {
int m, ci;
vector<int> a;
};
struct node {
int u, v, w;
bool operator < (const node &r) const {
return w < r.w;
}
};
vector<point> vp;
vector<buy> vb;
vector<node> vn, vt;
vector<int> dsu;
int Find(int x) {
if(x == dsu[x]) return x;
return dsu[x] = Find(dsu[x]);
}
bool Union(int x, int y) {
int a = Find(x), b = Find(y);
if(a != b) {
dsu[a] = b;
return true;
}
return false;
}
int ksu(int t, vector<node> &vn, vector<node> &used) {
if(t == 1) return 0;
int m = vn.size(), ans = 0;
used.clear();
for(int i = 0; i < m; ++i) {
if(Union(vn[i].u, vn[i].v)) {
ans += vn[i].w;
used.push_back(vn[i]);
if(--t == 1) break;
}
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
USE_CPPIO();
int T;
cin >> T;
while(T--) {
vp.clear();
vb.clear();
vn.clear();
dsu.clear();
int n, q;
cin >> n >> q;
for(int i = 0; i < q; ++i) {
buy tmp;
cin >> tmp.m >> tmp.ci;
for(int j = 0; j < tmp.m; ++j) {
int t;
cin >> t;
tmp.a.push_back(t - 1);
}
vb.push_back(tmp);
}
for(int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
vp.push_back({x, y});
}
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
int dx = vp[i].x - vp[j].x, dy = vp[i].y - vp[j].y;
vn.push_back({i, j, dx * dx + dy * dy});
}
}
sort(vn.begin(), vn.end());
for(int i = 0; i < n; ++i) dsu.push_back(i);
int ans = ksu(n, vn, vt);
for(int mask = 0; mask < (1 << q); ++mask) {
dsu.clear();
for(int i = 0; i < n; ++i) dsu.push_back(i);
int cnt = n, c = 0;
for(int i = 0; i < q; ++i) {
if(mask & (1 << i)) {
c += vb[i].ci;
for(int j = 1; j < vb[i].a.size(); j++) {
if(Union(vb[i].a[0], vb[i].a[j]))
--cnt;
}
}
}
vector<node> vd;
ans = min(ans, c + ksu(cnt, vt, vd));
}
cout << ans << endl;
if(T) cout << endl;
}
return 0;
}
|