UV 1508-Equipment state compression enumeration subset dfs

Source: Internet
Author: User

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<
     
      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.