Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D
题意:
有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值。除此之外,还有一些规则,x,y,w代表的是如果x吃完后吃y,那么还能获得额外的w欢乐值。计算所能获得的最大欢乐值。
思路:
看到别人说要用状压dp来做,我才恍然大悟啊,感觉自己对于状压dp实在是太不敏感了。
d【i】【j】表示在当前i状态时最后一份吃的是j的最大欢乐值。
状态转移什么的请看代码吧。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; ll n, m, t;
ll val[maxn];
ll d[<<][maxn], r[maxn][maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%lld%lld%lld",&n,&m,&t))
{
for(int i=;i<n;i++) scanf("%lld",&val[i]); memset(r,,sizeof(r));
for(int i=;i<t;i++)
{
int x,y; ll w;
scanf("%d%d%lld",&x,&y,&w);
x--; y--;
r[x][y]=w;
} memset(d,-,sizeof(d));
for(ll i=;i< n;i++)
{
d[<<i][i]=val[i];
} ll ans=; for(ll i=;i<(<<n);i++)
{
int cnt=;
ll tmp=i;
while(tmp) //计算出i状态已经吃了几份菜了
{
if(tmp&) cnt++;
tmp>>=;
} for(ll j=;j<n;j++)
{
if(cnt==m && (i&(<<j))) ans=max(ans,d[i][j]); //如果已经吃了m份菜,分别求最后吃的是j的时候的欢乐值,维护最大值
if(cnt==m || !(i&(<<j))) continue; //如果还没够m份,接下计算吃k时所能获得的最大欢乐值
for(ll k=;k<n;k++)
{
if(i&(<<k)) continue;
d[(<<k)|i][k]=max(d[(<<k)|i][k],d[i][j]+val[k]+r[j][k]);
}
}
}
printf("%lld\n", ans);
}
return ;
}
Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)的更多相关文章
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp
题目链接: http://codeforces.com/problemset/problem/401/D D. Roman and Numbers time limit per test4 secon ...
- Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)
用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.操作最多有84934656,514ms跑过,实际远远没这么多. 脑补过一下费用流, ...
- Codeforces Round #384 (Div. 2) E. Vladik and cards 状压dp
E. Vladik and cards 题目链接 http://codeforces.com/contest/743/problem/E 题面 Vladik was bored on his way ...
- CF580D Kefa and Dishes 状压dp
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. Th ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #321 (Div. 2) C. Kefa and Park dfs
C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company 二分
B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...
随机推荐
- java类的初始化的默认值
转自:http://blog.csdn.net/abc5382334/article/details/18254517 对于类的成员变量 不管程序有没有显示的初始化,Java 虚拟机都会先自动给 ...
- Android - 采用 SharedPreferences 存储数据
SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data/data/<包名& ...
- PHP unlink()函数,删除文件
定义和用法 unlink() 函数删除文件. 若成功,则返回 true,失败则返回 false. 语法 unlink(filename,context) 参数 描述 filename 必需.规定要删除 ...
- Feature Toggle JUnit
Feature Toggle,简单来说,就是一个开关,将未完成功能的代码屏蔽后发布到生产环境,从而避免多分支的情况.之所以有本文的产生,就是源于此情景.在引入Feature Toggle的同时,我们发 ...
- Oracle数据类型clob和blob
clob(size)变长 字符型大对象,最大8tb 应用场景:如果varchar2不够用了使用clob blob(size)变长 二进制数据,可以存放图片声音 8tb 但是,实际工作中很少把图片和声音 ...
- type="submit"
<?php var_dump($_REQUEST); ?> <form action="" id="javascript_page"> ...
- GraphicsMagick 号称图像处理领域的瑞士军刀
标签: librarydelegatesimage图像处理fontstiff 2012-09-13 10:15 2496人阅读 评论(0) 收藏 举报 分类: java技术(52) 简介 ...
- python及numpy,pandas易混淆的点
https://blog.csdn.net/happyhorizion/article/details/77894035 初接触python觉得及其友好(类似matlab),尤其是一些令人拍案叫绝不可 ...
- Python基础之软件目录结构规范
设计一个层次清晰的目录结构,就是为了达到这两点: 可读性高:不熟悉项目的人,一眼就能看懂目录结构. 可维护性高:随着时间的推移,代码/配置的规模增加,项目结构不会混乱,仍然能够组织良好. 目录组织方式 ...
- 修改nginx的http响应头server字段
信息泄露类型:HTTP服务器响应头Server字段信息泄露 示例: 解决: 需要重新对nginx编译安装: [root@localhost ~]# tar zxvf nginx-1.8.1.tar.g ...