B1076 [SCOI2008]奖励关 状压dp&&期望dp

这个题的n<15,一看就是状压dp。但是状态不是很好想。f[][]存i关的状态j。

这个题另一个关键思想在于倒推,我一开始想的是正推,但是只能记忆化了。

题干:

题目描述
  你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关。在这个奖励关里,系统将依次随机抛出k次宝物,
每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再吃)。
 宝物一共有n种,系统每次抛出这n种宝物的概率都相同且相互独立。也就是说,即使前k-1次系统都抛出宝物1(
这种情况是有可能出现的,尽管概率非常小),第k次抛出各个宝物的概率依然均为1/n。 获取第i种宝物将得到Pi
分,但并不是每种宝物都是可以随意获取的。第i种宝物有一个前提宝物集合Si。只有当Si中所有宝物都至少吃过
一次,才能吃第i种宝物(如果系统抛出了一个目前不能吃的宝物,相当于白白的损失了一次机会)。注意,Pi可
以是负数,但如果它是很多高分宝物的前提,损失短期利益而吃掉这个负分宝物将获得更大的长期利益。 假设你
采取最优策略,平均情况你一共能在奖励关得到多少分值?

输入格式
  第一行为两个正整数k和n,即宝物的数量和种类。以下n行分别描述一种宝物,其中第一个整数代表分值,随
后的整数依次代表该宝物的各个前提宝物(各宝物编号为1到n),以0结尾。

输出格式
  输出一个实数,保留六位小数,即在最优策略下平均情况的得分。

样例输入
1 2
1 0
2 0
样例输出
1.500000
提示
【数据规模】
1<=k<=100,1<=n<=15,分值为[-10^6,10^6]内的整数。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
int n,k;
int need[20],sc[20];
db f[102][1 << 16];
int main()
{
    read(k);read(n);
    duke(i,1,n)
    {
        read(sc[i]);
        int x;
        while(scanf("%d",&x) && x != 0)
        {
            need[i] |= 1 << (x - 1);
        }
    }
    lv(i,k,0)
    {
        duke(j,0,(1 << n) - 1)
        {
            duke(l,1,n)
            {
                if(!((~j) & need[l]))
                {
                    f[i][j] += max(f[i + 1][j],f[i + 1][j | (1 << (l - 1))] + sc[l]);
                }
                else
                {
                    f[i][j] += f[i + 1][j];
                }
            }
            f[i][j] /= n;
        }
    }
    printf("%.6lf
",f[1][0]);
    return 0;
}
/*
1 2
1 0
2 0
*/
原文地址:https://www.cnblogs.com/DukeLv/p/9674995.html