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 ...
随机推荐
- jhipser微服务架构介绍
内容提要 本文涉及以下内容: 微服务架构介绍 spring cloud介绍 jhipster架构介绍 微服务架构介绍 微服务概念 微服务和SOA很相似,都是按照业务功能把系统拆分成一个一个的服务.比如 ...
- 【BZOJ3280】小R的烦恼 最小费用最大流
[BZOJ3280]小R的烦恼 Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题. 问题是这样的 ...
- angularJS指令系统---Directive
指令:Directive angularJS 有一套完整的,可拓展的,用来帮助web应用开发的指令集: 在建立DOM期间,和HTML关联着的指令会被检测到,并被执行: 在angularJS中将前缀为 ...
- 手机相册管理(gallery) ---- HTML5+
模块:gallery Gallery模块管理系统相册,支持从相册中选择图片或视频文件.保存图片或视频文件到相册等功能.通过plus.gallery获取相册管理对象. 管理我们手机上用到的相册:选择图片 ...
- 170725、Kafka原理与技术
本文转载自:http://www.linkedkeeper.com/detail/blog.action?bid=1016 Kafka的基本介绍 Kafka最初由Linkedin公司开发,是一个分布式 ...
- JavaCSV之读CSV文件
Java在进行数据处理,有时候难免有进行CSV文件的操作,这里采用了JavaCSV读CSV文件. 1.准备工作 (1)第三方包库下载地址:https://sourceforge.net/project ...
- ClassicLink互通原理
ClassicLink概述_ClassicLink_用户指南_专有网络 VPC-阿里云 https://help.aliyun.com/document_detail/65412.html Class ...
- Wormholes---poj3259(最短路 spfa 判断负环 模板)
题目链接:http://poj.org/problem?id=3259 题意是问是否能通过虫洞回到过去: 虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts. 我们把虫洞看成是一条负权路,问 ...
- 转载:Linux内核调试方法
转载文章请注明作者和二维码及全文信息. 转自:http://blog.csdn.net/swingwang/article/details/72331196 不会编程的程序员,不是好的架构师,编程和内 ...
- 用Python实现的数据结构与算法:双端队列
一.概述 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的线性数据结构.双端队列也拥有两端:队首(front).队尾(rear),但与队列不同的是,插入操作在两 ...