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 ...
随机推荐
- bootstrap之按钮和图片
一.按钮 类 描述 .btn 为按钮添加基本样式 .btn-default 默认/标准按钮 .btn-primary 原始按钮样式(未被操作) .btn-success 表示成功的动作 .btn-in ...
- Web和Native使用外部字体ttf方法
论坛教程:http://bbs.egret.com/forum.php?mod=viewthread&tid=24879&extra=&highlight=%E5%AD%97% ...
- cocos2dx3.1从零学习(二)菜单、场景切换、场景传值
转:http://www.it165.net/pro/html/201406/16195.html 回顾一下上一篇的内容,我们已经学会了创建一个新的场景scene,添加sprite和label到层中, ...
- gradle多项目 svn依赖
当svn有多个子项目且没有根项目的时候,用eclipse拷贝下来是容易出问题的,经常子项目之间的依赖会有问题,还是推荐用IDEA. 操作说明: 如果SVN有 A,B,C,D四个项目,A为web项目,B ...
- SpringMVC实现简单应用
我们都知道,servlet代码一般来说只能在一个servlet中做判断去实现一个servlet响应多个请求, 但是springMVC的话还是比较方便的,主要有两种方式去实现一个controller里能 ...
- mysql 把表中某一列的内容合并为一行
1,把表中某一列的内容合并为一行 select province,CONCAT('[\"全部\",\"',GROUP_CONCAT(city ORDER BY cityI ...
- Hive sql语法详解
Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的SQ ...
- 转载 io多路复用
作者:ZingpLiu 出处:http://www.cnblogs.com/zingp/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 回到 ...
- android 本地通知
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notificat ...
- 《TP5.0学习笔记---模型篇》
https://blog.csdn.net/self_realian/article/details/78596261 一.什么是模型 为什么我们要在项目中使用模型,其实我们知道,我们可以直接在控制器 ...