10817-headmaster ' s headache (the headmaster's annoyance)

Source: Internet
Author: User
Tags bitwise operators

Classic State compression DP.

According to the DP stage definition, we need to enumerate each teacher for recursion, but because each teacher can teach the course is complex and diverse, so that the state becomes difficult to transfer. So what does it say about state? Obviously it is not possible to add one or two dimensions, so we can use a binary enumeration subset to act as a set with a single integer through a bitwise operation. The bitwise operators provided by C + + are very much like operations on collections, and we happen to be able to take advantage of this.

Use D[I][S1][S2] to indicate the minimum cost of taking into account the first I person. S1 says that there is a set of subjects that a person teaches, and S2 that there are at least two subjects to teach.  Because these two quantities can be introduced by a person also does not teach the collection of subjects, so with these two quantities can already represent all States. Then there are all sorts of wonderful bit operations, the hardest part of the code is probably the two-bit operations:

int M0 = St[i] & S0, m1 = St[i] & s1; S0 ^= M0; S1 = (S1 ^ m1) | M0; S2 |= M1;
Now that you want to choose the current teacher, the lessons he teaches will make the set change. M0 is a class he can teach and no one teaches at the moment, M1 refers to a class that he can teach and that only one person teaches at present.  The ^ operator makes the same 0, different from 1, so that the classes he can teach become 0 in the set S0, which means that these classes are upgraded to one-person class. So obviously to update the S1, (S1 ^ M1) and the role of the previous said, so that these classes to upgrade to at least two people to teach, but do not forget that there are some classes upgraded to just one person to teach, so with the | (or) operation, add these lessons to S1, S2 of course to add S1 in the upgraded elements.

Bit arithmetic not only saves space, but also saves time, so it is worthwhile to spend some time practicing using bit arithmetic to write code.

#include <bits/stdc++.h>using namespace Std;const int INF = 100000000;int s,n,m,a,c[125],st[125],d[125][1< <8][1<<8];int dp (int i,int s0,int s1,int s2) {if (i = = m+n) return s2 = = (1<<s)-1? 0:inf;//if the requirement is still not met    , no solution int& ans = d[i][s1][s2];    if (ans >= 0) return ans;    ans = INF; if (i >= m) ans = DP (I+1,S0,S1,S2);//Do not select the teacher int M0 = st[i] & S0, m1 = St[i] & s1; The bitwise operation of the set is extremely classic. Must master S0 ^= M0; S1 = (S1 ^ m1) | M0;    S2 |= M1; ans = min (Ans,c[i] + DP (I+1,S0,S1,S2));//select return ans;}        int main () {while (~scanf ("%d%d%d", &s,&m,&n) &&s) {memset (d,-1,sizeof (d));            for (int i=0;i<m+n;i++) {scanf ("%d", &c[i]);            St[i] = 0;                while (true) {scanf ("%d", &a); a--; St[i] = St[i] |                (1 << a);//Add the element to the collection in char C = GetChar (); if (c = =) break;//ASCII code for newline}} int ans = DP (0, (1<<s)-1, 0,0);    printf ("%d\n", ans); } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

10817-headmaster ' s headache (the headmaster's annoyance)

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.