codeforces 580D Kefa and Dishes
2015-09-25 11:40
393 查看
传送门:http://codeforces.com/problemset/problem/580/d
思路:状压DP,f[i][j]表示最后一个为i,已选取的菜的状态为j。
思路:状压DP,f[i][j]表示最后一个为i,已选取的菜的状态为j。
#include<cstdio> #include<cstring> #include<algorithm> const int maxt=540000; using namespace std; typedef long long ll; int n,m,k,g[20][20],a[20],pow[20];ll f[20][maxt],ans; int main(){ pow[0]=1;for (int i=1;i<=19;i++) pow[i]=pow[i-1]<<1; scanf("%d%d%d",&n,&m,&k); for (int i=0;i<n;i++) scanf("%d",&a[i]); for (int i=1,x,y,z;i<=k;i++) scanf("%d%d%d",&x,&y,&z),x--,y--,g[x][y]=z; for (int i=0;i<n;i++) f[i][pow[i]]=a[i]; for (int i=1;i<m;i++) for (int s=0;s<pow ;s++){ int cnt=0; for (int j=0;j<n;j++) if (s&pow[j]) cnt++; if (cnt!=i) continue; for (int j=0;j<n;j++) if (!(s&pow[j])){ for (int k=0;k<n;k++) if (s&pow[k]) f[j][s|pow[j]]=max(f[j][s|pow[j]],f[k][s]+a[j]+g[k][j]); } } for (int s=0;s<pow ;s++){ int cnt=0; for (int j=0;j<n;j++) if (s&pow[j]) cnt++; if (cnt!=m) continue; for (int i=0;i<n;i++) ans=max(ans,f[i][s]); } printf("%I64d\n",ans); return 0; }
相关文章推荐
- classLoader和javassist
- 算法之每日一题:哥德巴赫猜想
- OWC 绘制3D柱状图
- git之 git checkout
- 网站渗透测试
- Android中Application、静态变量和Sharedpreferences的使用与区别
- 内核中的list
- LeetCode---Majority Element
- php end()函数与current()函数
- mysql合理配置连接池数量
- PRML第一章_易混淆概念-先验后验&生成判别&分类回归
- Cocos2d-x 使用物理引擎进行碰撞检测
- c#获取目录2
- Makefile好助手:pkgconfig
- 中国朝代
- Codeforces580D. Kefa and Dishes
- windows32位下安装Redis+连接PHP
- Android万能适配器
- Android UI学习 - Menu
- Oracle数据库导入、导出问题。