Codeforces 580D Kefa and Dishes(状压DP)
2024-10-06 05:44:05
题目大概说要吃掉n个食物里m个,吃掉各个食物都会得到一定的满意度,有些食物如果在某些食物之后吃还会增加满意度,问怎么吃满意度最高。
- dp[S][i]表示已经吃掉的食物集合是S且刚吃的是第i个食物的最大满意度
。。没什么好说的
#include<cstdio>
#include<algorithm>
using namespace std; int val[],pairs[][];
long long d[<<][]; int getCnt(int s){
int cnt=;
for(int i=; i<; ++i){
if(s>>i&) ++cnt;
}
return cnt;
} int main(){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<n; ++i){
scanf("%d",val+i);
}
int a,b,c;
while(k--){
scanf("%d%d%d",&a,&b,&c);
--a; --b;
pairs[a][b]=c;
}
for(int i=; i<n; ++i){
d[<<i][i]=val[i];
}
for(int s=; s<(<<n); ++s){
for(int i=; i<n; ++i){
if((s>>i&)==) continue;
for(int j=; j<n; ++j){
if(i==j || (s>>j&)==) continue;
d[s][i]=max(d[s][i],d[s^(<<i)][j]+val[i]+pairs[i][j]);
}
}
}
long long res=;
for(int s=; s<(<<n); ++s){
if(getCnt(s)!=m) continue;
for(int i=; i<n; ++i){
res=max(res,d[s][i]);
}
}
printf("%lld",res);
return ;
}
最新文章
- 微软unity 注入mvc
- C#以管理员身份运行程序
- 经典算法和OJ网站(开发者必备-转)
- jq 解析josn字符串
- Linux nmap
- Linux C 程序 信号及信号的处理(19)
- [Everyday Mathematics]20150126
- VIM中文乱码
- H面试程序(15): 冒泡排序法
- Codeforces 489A	SwapSort
- codeforces 659B Qualifying Contest
- python绝技 — 使用PyGeoIP关联IP地址和物理位置
- 【百度之星2014~初赛(第二轮)解题报告】JZP Set
- shell 中 if then语句中会跟着-ne -ge之类的参数的含义
- 为什么我们要使用int类型来保存时间类型的数据。
- 【css技能提升】完美的 Sticky Footer 布局
- C语言实现简单CMDShell
- Executor简介
- How to Pronounce EVERY
- lua中table如何安全移除元素