UV 1508-Equipment state compression enumeration subset dfs
ACM
Question address: Ultraviolet A 1508-Equipment -- PDF
Question:
N 5-tuples are given, and k groups are selected from them, so that the sum of the five locations in each of these groups is the largest.
Analysis:
After thinking for a long time, I finally referred to others' questions...
However, the idea is great and worth learning.
Since the range of n is, it is difficult to consider n.
Therefore, we will consider the 5-tuples.
Each group of 5 tuples may be selected as a part of the sum, that is, [11111], that is, a subset of [all selected and]. There are 31 cases in total.
We only need to pre-process these 31 conditions to obtain the largest possible sum. Then the dfs traverses the subset. For details, see the code.
Code:
/** Author: illuz
* File: 1508.cpp* Create Date: 2014-06-28 20:55:20* Descripton: */#include
#include
#include
#include using namespace std;const int N = 10010;int n, t, k, ans;int m[5], r[N][5], mmax[40];int dfs(int S, int num) {// find num different subset in S, return the max sumif (num == 0) {return 0;}int tmp = 0;for (int S0 = S; S0; S0 = (S0-1)&S) {tmp = max(tmp, mmax[S0] + dfs((S0^S), num - 1));}return tmp;}int main() {scanf("%d", &t);while (t--) {memset(m, 0, sizeof(m));// inputscanf("%d%d", &n, &k);for (int i = 0; i < n; i++) {for (int j = 0; j < 5; j++) {scanf("%d", &r[i][j]);m[j] = max(m[j], r[i][j]);}}if (k >= 5) {// just the maxint sum = 0;for (int i = 0; i < 5; i++) {sum += m[i];}printf("%d\n", sum);} else {memset(mmax, 0, sizeof(mmax));for (int i = 0; i < n; i++) {// for every onefor (int S = 0; S <= 31; S++) {// every situation, 00000 to 11111int tmp = 0;for (int k = 0; k < 5; k++) {if (S&(1<