CF580D Kefa and Dishes
题解
这题是用来智力康复的。。。
设
f
[
S
]
[
i
]
f[S][i]
f[S][i]为当前已选菜的状态为S,最后一个菜为i的最大满意度
然后转移即可
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k, a[25], val[25][25], f[(1<<18)+5][25];
signed main(){
scanf("%lld%lld%lld", &n, &m, &k);
for(int i = 1; i <= n; i ++) scanf("%lld", &a[i]), f[1ll << (i - 1)][i] = a[i];
for(int i = 1; i <= k; i ++){
int x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
val[x][y] = z;
}
for(int S = 0; S < (1 << n); S ++){
for(int i = 1; i <= n; i ++) if((1 << (i - 1)) & S) {
for(int j = 1; j <= n; j ++) if(((1 << (j - 1)) & S) == 0){
int S1 = S | (1 << (j - 1));
f[S1][j] = max(f[S1][j], f[S][i] + a[j] + val[i][j]);
}
}
}
int ans = 0;
for(int S = 0; S < (1 << n); S ++){
int ret = 0;
for(int i = 1; i <= n; i ++) if(S & (1 << (i - 1))) ret ++;
if(ret == m){
for(int i = 1; i <= n; i ++)
ans = max(ans, f[S][i]);
}
}
printf("%lld", ans);
return 0;
}
智力仍需康复