Cf580D. Kefa and Dishes (shape pressure dp)

Keywords: C++

meaning of the title

$n $food, each food has a satisfaction, from which choose $m $to make the satisfaction maximum.

There is also a $k $relationship: if $x_i $is eaten before $y_i $it will cost $C_i $

Sol

The official question was $O (2 ^ n ^ 2)$, but I didn't find any connection between states, so I wrote a $O (2 ^ n ^ 3)$one, but the water passed.

$f[i][j][sta] $means that $i $has been released now, and $j $will be placed in this round, in the state of $sta.$

When transferring, enumerate what was put in the last one.

/*
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define LL long long 
#define rg register 
#define sc(x) scanf("%d", &x);
#define pt(x) printf("%d ", x);
#define db(x) double x 
#define rep(x) for(int i = 1; i <= x; i++)
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
char obuf[1<<24], *O = obuf;
#define OS  *O++ = ' ';
using namespace std;
using namespace __gnu_pbds;
const int MAXN = 1e6 + 10, INF = 1e9 + 10, mod = 1e9 + 7;
const double eps = 1e-9;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
void print(int x) {
    if(x > 9) print(x / 10);
    *O++ = x % 10 + '0';
}
int N, M, K;
int f[2][19][262145];
int lk[19][19], a[MAXN];
main() {
    N = read(); M = read(); K = read();
    for(int i = 1; i <= N; i++) a[i] = read();
    for(int i = 1; i <= K; i++) {
        int x = read(), y = read(), z = read();
        lk[x][y] = z;
    }
    int lim = (1 << N) - 1, o = 0;
    for(int i = 1; i <= M; i++) {
        o ^= 1;
        for(int sta = 0; sta <= lim; sta++) {
            if((__builtin_popcount(sta)) != i) continue;
            for(int j = 1; j <= N; j++) {//��һ�ֳԵ�ɶ 
                if(!(sta & (1 << j - 1))) continue;
                for(int k = 1; k <= N; k++) {//��һ�ֳԵ�ɶ 
                    if(sta & (1 << k - 1)) {
                        /*if(o == 0 && j == 2 && k == 1) {
                            puts("GG");
                        }*/
                        f[o][j][sta] = max(f[o][j][sta], f[o ^ 1][k][sta ^ (1 << j - 1)] + lk[k][j]);
                    }
                }                
            }
        }            
    }
//    printf("%d\n", f[o][2][lim]);
    
    int ans = 0;
    for(int i = 1; i <= N; i++) {
        for(int sta = 0; sta <= lim; sta++) {
            if((__builtin_popcount(sta)) != M) continue;
            if(!(sta & (1 << i - 1))) continue;
            int now = f[o][i][sta];
            for(int j = 1; j <= N; j++) 
                if(sta & (1 << j - 1)) now += a[j];
            ans = max(ans, now);
        }
    }
    cout << ans;
    return 0;
}
/*
2 2 1
1 1
2 1 1
*/

Posted by sdizier on Sun, 03 Feb 2019 16:00:16 -0800