UVA 10817 Headmaster's Headache

状态压缩dp,用两位二进制来表示一门课的信息。



code:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define clr(a,b) memset((a),b,sizeof(a))
const int maxn = 10010;
typedef long long lld;
const int inf = 0x3f3f3f3f;
int dp[1<<16];
int a[10];
int n,m,s;
int get(int S)
{
    int res=0;
    for(int i=0;i<s;i++)
    {
        int tmp=(S>>(2*i))&3;
        tmp+=a[i];
        if(tmp>=2) tmp=3;
        res|=(tmp<<(2*i));
    }
    return res;
}
int main()
{
    while(~scanf("%d %d %d",&s,&m,&n)&&s)
    {
        int sum=0;
        clr(a,0);
        clr(dp,inf);
        for(int i=0;i<m;i++)
        {
           int c;
           char si;
           scanf("%d",&c);
           sum+=c;
           while(1)
           {
               char ch=getchar();
               if(ch!=' ') break;
               int t;
               scanf("%d",&t);
               a[t-1]++;
           }
        }
        int all=(1<<(s+s))-1;
        int S=get(0);
        dp[S]=sum;
        for(int i=0;i<n;i++)
        {
            int c;
            scanf("%d",&c);
            clr(a,0);
            while(1)
            {
               char ch=getchar();
               if(ch!=' ') break;
               int t;
               scanf("%d",&t);
               a[t-1]++;
            }
            for(int j=all;j>=0;j--)
            {
                if(dp[j]==inf) continue;
                S=get(j);
                dp[S]=min(dp[S],dp[j]+c);
            }
        }
        printf("%d\n",dp[all]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值