ACM-ICPC 2018 南京赛区网络预赛 E. AC Challenge (状态压缩DP)
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=13points.
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=55points 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道题,每道题做出来会得到t*a[i]+b[i]分,但是有些题目有先决条件,需要先完成某些题目才能写,问最多能得到多少分
题目思路:这道题需要用二进制的做法。首先先用二进制表示每道题的先决条件,放入pre数组,第几位是1就是需要先写第几题。然后就把所有的情况全部枚举出来,由于一共就20题,一共也就2^20的情况,然后也是用二进制表示每一种情况。由于是从小到大,所以他的前一刻一定都已经出来了,然后就试探把每一位删掉,判断这道题需要做的先决条件是否已经够了,先&pre[i],如果能够等于pre[i],这说明需要的题目都已经出了,可以推出当前情况,然后数出这种情况是第几个1,也就是说这道题是哪一刻做的,然后就可以算出这种情况下的值。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f ; #define ll long long ll dp[(1<<21)]; int One[1<<21]; struct no { int a,b,id,per; }a[21]; ///得到n的二进制的1,表示的是一共做了多少了 int GetOne(int n) { int count = 0; while(n){ count++; n = n & (n - 1); } return count; } int main( ) { int n ; scanf("%d",&n); for(int i=0 ; i<n ; i++) { scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].id); while(a[i].id--) { int x ; scanf("%d",&x); a[i].per |= (1<<(x-1)); ///per记录了这个问题需要完成谁 } } memset(dp,-INF,sizeof(dp)) ; ll ans = 0; for(int i=0 ; i<(1<<n) ; i++) One[i]=GetOne(i); dp[0] = 0 ; for(int i=0 ; i<(1<<n) ; i++) { if(dp[i]!=-INF) ///减少重复运算 { for(int j=0 ; j<n ; j++) { if((i & a[j].per)==a[j].per)///如果满足了这个问题的条件 { if((i&(1<<j))==0)///如果j问题还没有用到 { dp[(i|(1<<j))] = max( dp[(i|(1<<j))] , dp[i]+(ll)(One[i]+1)*a[j].a+a[j].b); } } } } ans = max(ans,dp[i]); } printf("%lld\n",ans); return 0 ; }