E. AC Challenge(ACM-ICPC 2018 南京賽區網絡預賽,狀態壓縮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 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

題目來源

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

思路

首先說題意。

要進行一場比賽,這場比賽中有n 道題目,每一道題目有兩個屬性ab ,完成這一道題目所獲得的分數爲t×a+b (t爲時間戳)。但是有一些要求,對於某一道題目,要完成它,必須完成一些前置題目,只有做了前置題目,纔可以完成當前的這道題。樣例是如下給出的。首先給出一個數字代表n ,接下來有n 行,對於每一行首先給出兩個數ab 代表這一道題目的兩個屬性,接下來給出一個數字s ,代表完成這道題目之前要完成多少道題目,接下來有s 個數字,代表這s 道題目的具體編號是多少.

題目問你,該如何規劃,可以使得獲得的分數最大,最後輸出這個分數。

我們可以看一下n 的範圍,發現只有20 ,所以可能是狀壓dp,我們利用二進制位爲1代表這個位置的題目做了,二進制位爲0代表當前位置的題目沒有做。

對於這個題目,我們做出如下定義(以下狀態均指二進制狀態):

  • dp[state] :代表解決的問題爲state這個狀態所能獲得的最大分數
  • pre[i] :表示要解決第i 個問題,需要解決的問題集合,集合用二進制的狀態表示
  • cnt[state] :表示state這個狀態已經解決的問題數量

那麼我們考慮如何利用狀態壓縮進行轉移.

遍歷 時間戳t=1...n
    遍歷 題目編號j=0...n-1
        遍歷 狀態k=0...(1<<n)-1
            如果 狀態k已經解決t-1個問題 且 狀態k沒有解決第j個問題 且 狀態k已經解決了狀態j的前置問題
                當k狀態解決j問題後所得的值 >= 總狀態數(跳過,因爲所有的狀態一共只有(1<<n))
                    continue
                如果當前狀態可以更新
                    更新當前狀態
                    記錄dp[狀態]中的最大值
輸出最大值       

代碼

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1 << 21;
/*
pre[i]:表示要解決第i個問題,需要解決的問題集合(二進制狀態)
dp[state]:表示解決當前狀態的問題最多能獲得的分數(二進制狀態)
cnt[state]:表示state這個狀態已經解決的問題數量
*/
ll a[N], b[N], pre[N];
ll dp[N], cnt[N], n;
void solve()
{
    dp[0] = 0;
    ll ans = 0;
    for (ll t = 1; t <= n; t++) //枚舉時間
    {
        for (ll j = 0; j < n; j++) //枚舉每道題目
        {
            for (ll k = 0; k < (1 << n); k++) //從0~2^n-1個狀態
            {
                //如果狀態k已經解決t-1個問題 且 狀態k沒有解決第j個問題 且 狀態k已經解決了狀態j的前置問題
                if (cnt[k] == t - 1 && !((k >> j) & 1) && (k & pre[j]) == pre[j])
                {
                    if ((k | (1 << j)) >= (1 << n)) //當k狀態解決j問題後所得的值 >= 總狀態數就跳過,因爲所有的狀態一共只有(1<<n)
                        continue;
                    if (dp[k] + a[j] * t + b[j] >= dp[(k | (1 << j))])
                    {
                        dp[k | (1 << j)] = dp[k] + a[j] * t + b[j];
                        cnt[k + (1 << j)] = cnt[k] + 1;
                        ans = max(ans, dp[k + (1 << j)]);
                    }
                }
            }
        }
    }
    printf("%lld\n", ans);
}
int main()
{
    //freopen("in.txt", "r", stdin);
    ll t, k;
    scanf("%lld", &n);
    for (ll i = 0; i < n; i++)
    {
        scanf("%lld%lld%lld", &a[i], &b[i], &t);
        while (t--)
        {
            scanf("%lld", &k);
            pre[i] |= (1 << (k - 1));
        }
    }
    solve();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章