[codeforces 580D]Kefa and Dishes
来源:互联网 发布:linux文件类型 编辑:程序博客网 时间:2024/06/01 10:26
n很小,很容易想到状态压缩dp,用dp[set][i]表示结尾是i,用了set里的元素的最大值,枚举set和i来更新下一个元素即可。
#include<iostream>#include<cstdio>#include<cstring>#include<set>using namespace std;long long val[20],dp[300000][20];long long n,m,k;set<long long> s;long long max(long long x,long long y){ return x>y?x:y;}long long decode(long long x){ long long ret=0; for(long long i=1;i<=n;i++){ if(x&1){s.insert(i);ret++;} x>>=1; } return ret;}long long r[20][20];int main(){ cin>>n>>m>>k; for(long long i=1;i<=n;i++)cin>>val[i]; long long x,a,b; memset(r,0,sizeof(r)); for(long long i=1;i<=k;i++){ cin>>a>>b>>x; r[a][b]=x; } long long len=1<<n;long long ans=-1; memset(dp,-1,sizeof(dp)); for(long long i=1;i<len;i++){ s.clear(); long long num=decode(i); for(set<long long>::iterator j=s.begin();j!=s.end();j++){ long long jj=*j;if(num==1)dp[i][jj]=val[jj]; if(num==m)ans=max(ans,dp[i][jj]); for(long long l=1;l<=n;l++) if(s.find(l)==s.end()) dp[i|(1<<(l-1))][l]=max(dp[i|(1<<(l-1))][l],dp[i][jj]+r[jj][l]+val[l]); } } cout<<ans<<endl; return 0;}
0 0
- Codeforces 580D Kefa and Dishes
- codeforces 580D Kefa and Dishes
- codeforces 580D. Kefa and Dishes dp
- [codeforces 580D]Kefa and Dishes
- codeforces 580D. Kefa and Dishes
- Codeforces 580D Kefa and Dishes
- codeforces 580 D.Kefa and Dishes(状压DP)
- codeforces 580 D. Kefa and Dishes (状压dp)
- codeforces 580D. Kefa and Dishes(状压dp)
- CodeForces 580D Kefa and Dishes(DP)
- codeforces 580D Kefa and Dishes【状态压缩+dp】
- Codeforces 580D Kefa and Dishes(dp+状态压缩)
- Codeforces 580D Kefa and Dishes【状压dp】
- codeforces 580D Kefa and Dishes (状压dp)
- CodeForces 580D.Kefa and Dishes(状压DP)
- CodeForces 580 D.Kefa and Dishes(状压DP)
- CodeForces 580D (状压DP) Kefa and Dishes
- Kefa and Dishes CodeForces
- 11 Container With Most Water
- ADO数据库编程
- code==200是什么意思
- 多线程
- Docker学习总结之Run命令介绍
- [codeforces 580D]Kefa and Dishes
- Codeforces Round #323 (Div. 2)C. GCD Table
- [codeforces 582b]Once Again…
- UVa 610
- 深入理解Javascript之this关键字
- 在BAT工作和创业公司工作有什么不同的体验?
- poj 2778(ac自动机+矩阵快速幂)
- 两个变量值的交换方法
- 使用系统提供的AVFoundation 实现二维码扫描(带动画)