題意 $n$個食物,每個食物有一個滿意度,從中選出$m$個,使得滿意度最大 同時有$k$個關係:若$x_i$在$y_i$之前吃,則會獲得$C_i$的代價 Sol 官方題解是$O(2^n n^2)$的,不過我沒發現狀態之間的聯繫,就寫了一個$O(2^n n^3)$的,不過還是水過去了。 $f[i][j ...
題意
$n$個食物,每個食物有一個滿意度,從中選出$m$個,使得滿意度最大
同時有$k$個關係:若$x_i$在$y_i$之前吃,則會獲得$C_i$的代價
Sol
官方題解是$O(2^n n^2)$的,不過我沒發現狀態之間的聯繫,就寫了一個$O(2^n n^3)$的,不過還是水過去了。
$f[i][j][sta]$表示現在已經放了$i$個,本輪要放第$j$個,狀態為$sta$
轉移的時候枚舉一下上一個放了什麼
/* */ #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 */