ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】

Enjoy_process發表於2018-09-05
  •  1000ms
  •  128536K

Dlsj is competing in a contest with n(0<n≤20) problems. And he knows the answer of all of these problems.

However, he can submit i-th problem if and only if he has submitted (and passed, of course) si​ problems, the pi,1​-th,pi,2​-th, ......, pi,si​​-th problem before.(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 i), he can get t×ai​+bi​ points. (∣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, n, which is the number of problems.

Then follows n lines, the i-th line contains si​+3 integers, ai​,bi​,si​,p1​,p2​,...,psi​​ as 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×5+6=11 points.

On the second minute, Dlsj submitted the second problem, and get 2×4+5=13 points.

On the third minute, Dlsj submitted the third problem, and get 3×3+4=13 points.

On the forth minute, Dlsj submitted the forth problem, and get 4×2+3=11 points.

On the fifth minute, Dlsj submitted the fifth problem, and get 5×1+2=7 points.

So he can get 11+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

樣例輸入

1
-100 0 0

樣例輸出2

0

題目來源

ACM-ICPC 2018 南京賽區網路預賽

題目大意:給n個問題,然後每個問題 給出a,b,s 分別表示 第i個解決這個題,就給 (a*i+b) 的得分,s表示有 s個前置問題,必須回答出來 s個前置問題 才能解決這個問題,求最大的得分,注意,可以不用答完所有的題

題解:狀態壓縮+DP。AC的C++程式碼:

#include<iostream>

using namespace std;
typedef long long ll;
const ll INF=1e18;
const int N=21;
struct Problem{
	ll a,b;
	int pre;
}p[N];

ll dp[1<<N];

int main()
{
	int n,s,x;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld%lld%d",&p[i].a,&p[i].b,&s);
		p[i].pre=0;
		while(s--){
			scanf("%d",&x);
			p[i].pre|=(1<<(x-1));
		}
	}
	for(int i=0;i<(1<<n);i++)//將所有狀態置為-INF 
	  dp[i]=-INF;
	dp[0]=0;
	ll res=0; 
	for(int i=0;i<(1<<n);i++){//遍歷所有狀態
		if(dp[i]==-INF)//如果這個狀態無法由初始狀態得來就跳過 
		  continue;
		//解題順序t等於已經解決的題數加一 
		int t=1; 
		//統計現在已經解決的題數,二進位為1的個數 
		for(int j=0;j<n;j++)
		  if(i&(1<<j))
		    t++;
		for(int j=0;j<n;j++){
			if(i&(1<<j)) continue;//如果第j到題已經做過了就跳過
           //如果第j道題還沒做,
		   //且要解決第j題所需先要解決的問題已經解決,就進行更新
           if((p[j].pre&i)==p[j].pre){
        		int tmp=i|(1<<j);//解決第j道題後的狀態
        		ll val=dp[i]+t*p[j].a+p[j].b;//解決第j道題後的總分 
			    dp[tmp]=max(dp[tmp],val);
			    res=max(res,dp[tmp]);
		   }   
		} 
	}
	printf("%lld\n",res); 
	return 0;
}

 

相關文章