Server Load balancer 10817-Headmaster's Headache (status compression dp)

Source: Internet
Author: User

Pi of each job seeker, for each job seeker, either or not, is the 01 backpack problem.

For s1 and s2, you can follow the courses of the currently enumerated job seekers to publish the next status:

NextS1 = p [I] | s1,

NextS2 = (p [I] & s1) | s2

F [nextS1] [nextS2] = min (f [nextS1] [nextS2], f [s1] [s2] + p [I])

 

/**========================================== *   This is a solution for ACM/ICPC problem * *   @problem: UVA 10817 - Headmaster's Headache *   @type:  dp *   @author: shuangde *   @blog: blog.csdn.net/shuangde800 *   @email: zengshuangde@gmail.com *===========================================*/#include<iostream>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<cmath>#include<cstring>using namespace std;typedef long long int64;const int INF = 0x3f3f3f3f;const double PI  = acos(-1.0);const int MAXN = 150;int courseNum, m, n, sum; int maxState;int f[1<<8][1<<8];int p[MAXN], c[MAXN], cnt[10];int dp(int st1, int st2){    memset(f, 0x3f, sizeof(f));    f[st1][st2] = sum;    for(int i=m+1; i<=n+m; ++i){        for(int s1=maxState; s1>=0; --s1){            for(int s2=maxState; s2>=0; --s2){                if(f[s1][s2] >= INF) continue;                int st1 = (p[i]|s1);                int st2 = (p[i]&s1) | s2;                f[st1][st2] = min(f[st1][st2], f[s1][s2]+c[i]);            }         }    }    return f[maxState][maxState];}int main(){    char str[1000];    while(~scanf("%d%d%d", &courseNum, &m, &n) && courseNum){            maxState = (1<<courseNum) - 1;        sum = 0;        int st1=0, st2=0;        memset(cnt, 0, sizeof(cnt));                for(int i=1; i<=m+n; ++i){            scanf("%d", &c[i]);            gets(str);            p[i] = 0;            for(int j=0; j<strlen(str); ++j){                if(isdigit(str[j])){                    int num = str[j] - '0';                    p[i] |= 1<<(num-1);                    if(i <= m) ++cnt[num-1];                }             }            if(i <= m){                 sum += c[i];                st1 |= p[i];            }        }         for(int i=0; i<courseNum; ++i)            if(cnt[i] > 1) st2 |= (1<<i);        printf("%d\n", dp(st1, st2));    }    return 0;} 

 

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.