题目大意:
有n到题目,每道题目有一个ai和bi代表分数,你做每道题目的分数为tai+bi,t为做这道题目的时间,规定了做每道题目前必须完成的其他题目,求获得的最大得分。
分析:
由于n是20,可以考虑状压dp复杂度为n2^n.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int maxn = 20 + 10;
typedef long long ll;
const ll INF = 1e18;
struct node
{
ll a,b;
}s[maxn];
int need[maxn];
ll dp[1<<21];
ll solve(int u)
{
ll num=0;
for(int i=0;i<=25;i++)
{
if(u&(1<<i)) num++;
}
return num;
}
int main()
{
int n,s1,v;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
memset(need,0,sizeof(need));
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&s[i].a,&s[i].b);
scanf("%d",&s1);
while(s1--)
{
scanf("%d",&v);
need[i]+=(1<<(v-1));
}
}
for(int i=0;i<(1<<n);i++) dp[i]=-INF;
ll ans=0;
dp[0]=0;
for(int i=0;i<(1<<n);i++)
{
for(int j=0;j<n;j++)
{
if(i&(1<<j))
{
int now=i-(1<<j);
if((now&need[j+1])==need[j+1])
{
dp[i]=max(dp[i],dp[now]+(solve(now)+1)*s[j+1].a+s[j+1].b);
ans=max(ans,dp[i]);
}
}
}
}
printf("%lld\n",ans);
return 0;
}