原题
Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems.
However, he can submit ii-th problem if and only if he has submitted (and passed, of course) s_isi problems, the p_{i, 1}pi,1-th, p_{i, 2}pi,2-th, ......, p_{i, s_i}pi,si-th problem before.(0 < p_{i, j} \le n,0 < j \le s_i,0 < i \le n)(0<pi,j≤n,0<j≤si,0<i≤n) After the submit of a problem, he has to wait for one minute, or cooling down time to submit another problem. As soon as the cooling down phase ended, he will submit his solution (and get "Accepted" of course) for the next problem he selected to solve or he will say that the contest is too easy and leave the arena.
"I wonder if I can leave the contest arena when the problems are too easy for me."
"No problem."
—— CCF NOI Problem set
If he submits and passes the ii-th problem on tt-th minute(or the tt-th problem he solve is problem ii), he can get t \times a_i + b_it×ai+bi points. (|a_i|, |b_i| \le 10^9)(∣ai∣,∣bi∣≤109).
Your task is to calculate the maximum number of points he can get in the contest.
Input
The first line of input contains an integer, nn, which is the number of problems.
Then follows nn lines, the ii-th line contains s_i + 3si+3 integers, a_i,b_i,s_i,p_1,p_2,...,p_{s_i}ai,bi,si,p1,p2,...,psias described in the description above.
Output
Output one line with one integer, the maximum number of points he can get in the contest.
Hint
In the first sample.
On the first minute, Dlsj submitted the first problem, and get 1 \times 5 + 6 = 111×5+6=11 points.
On the second minute, Dlsj submitted the second problem, and get 2 \times 4 + 5 = 132×4+5=13 points.
On the third minute, Dlsj submitted the third problem, and get 3 \times 3 + 4 = 133×3+4=13 points.
On the forth minute, Dlsj submitted the forth problem, and get 4 \times 2 + 3 = 114×2+3=11 points.
On the fifth minute, Dlsj submitted the fifth problem, and get 5 \times 1 + 2 = 75×1+2=7 points.
So he can get 11+13+13+11+7=5511+13+13+11+7=55 points in total.
In the second sample, you should note that he doesn't have to solve all the problems.
样例输入1复制
5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4
样例输出1复制
55
样例输入2复制
1 -100 0 0
样例输出2复制
0
题目来源
题目大意
有n个题目(n<=20),每个题目两个跟分数有关的属性a和b,每分钟都做且只做1道题,设一道题在第t分钟做,则这个题的得分公式为:a*t+b。注意:a或b可能是负数。
而且有些题目需要先完成另外一些题目(由数据给出)才可以提交这个题(题目保证满足拓扑结构,也就是无环)
问最大得分是多少。
思路
因为题目个数不超过20,所以枚举每个题目做和不做,最多有2的20次方种情况,也就是1e6的情况。以二进制形式枚举每个题目做/不做。设枚举变量为st,则0<= st < (1<<20)。
对于每种状态,枚举所有可以做且还没做的题目,进行状态转移。
设由状态st做了题i之后转移到的下一个状态为next,当前状态为st,枚举题目的变量为i。dp[x]表示到达状态x可以得到的最大分数。
则有状态转移方程:
dp[next]=max(由其它状态转移到next的分数,由st做了第i个题后转移到next的分数);
有初始化条件:dp[0]=0,表示一个题也不做分数为0
有答案:ans=max(dp[next])表示所有情况中最大分数。
具体细节见代码注释
AC代码
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
struct node{
int a,b;
};
node co[30];
int dp[1<<21];
int main(){
int vec[30];
int n,c,x;
cin>>n;
for(int i=0;i<n;i++){
scanf("%d%d",&co[i].a,&co[i].b);
scanf("%d",&c);
vec[i]=0;
for(int j=0;j<c;j++){
scanf("%d",&x);
vec[i]|=(1<<(x-1));//限制条件状压
}
}
#define inf 0x3f3f3f3f
memset(dp,-inf,sizeof(dp));//有负数
int maxst=1<<20;
int t;
int maxv=0;//最大分值。
dp[0]=0;
for(int st=0;st<maxst;st++){
if(st==-inf) //剪枝
continue;
t=1;
for(int j=st;j;j>>=1){
t+=j&1;//1的个数就是时间t
}
for(int i=0;i<n;i++){
if(st>>i&1)
continue;//第i题已经做过了
if((st|vec[i])>st)//是否可以做第i题
continue;//不可以做第i题
//完全满足做第i题的条件。
int next=st|(1<<i);//做完第i题的状态
//dp[next]=max(由其它状态转移到next的分数,由st转移到next的分数);
dp[next]=max(dp[next],dp[st]+t*co[i].a+co[i].b);
maxv=max(maxv,dp[next]);
}
}
cout<<maxv<<endl;
}