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
| #include <iostream> #include <cstring> #include <cmath> using namespace std; int N, ans = 1e9; bool selected[11]; int people[11]; int city[11][11]; bool check[11]; void getDifference() { int a_sum = 0, b_sum = 0; for (int i = 1; i <= N; ++i) { if (selected[i]) a_sum += people[i]; else b_sum += people[i]; } int res = abs(a_sum - b_sum); if (res < ans) ans = res; } void DFS(int n, int status) { check[n] = true; for (int i = 1; i <= N; ++i) { if (check[i]) continue; if (status == 1) { if (selected[i] && city[n][i]) { check[i] = true; DFS(i, status); } } else { if (!selected[i] && city[n][i]) { check[i] = true; DFS(i, status); } } } } void Solve() { memset(check, 0, sizeof(check)); for (int i = 1; i <= N; ++i) { if (selected[i]) { DFS(i, 1); break; } } for (int i = 1; i <= N; ++i) { if (!selected[i]) { DFS(i, 0); break; } } for (int i = 1; i <= N; ++i) { if (!check[i]) return; } getDifference(); } void SelectArea(int n, int cnt) { if (cnt >= N) return; if (cnt >= 1) { Solve(); } for (int i = n; i <= N; ++i) { if (selected[i]) continue; selected[i] = true; SelectArea(i, cnt + 1); selected[i] = false; } } int main() { cin >> N; for (int i = 1; i <= N; ++i) { cin >> people[i]; } for (int i = 1; i <= N; ++i) { int cnt; cin >> cnt; for (int j = 0; j < cnt; ++j) { int n; cin >> n; city[i][n] = 1; } } SelectArea(1, 0); if (ans == 1e9) ans = -1; cout << ans << "\n"; return 0; }
|