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 ...
随机推荐
- 【黑金ZYNQ7000系列原创视频教程】01.熟悉vivado——纯逻辑led实验
黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36627&extra=page%3D1 爱奇艺地址: http: ...
- Jboss AS 7 部署web应用程序时无法初始化spring的bean的解决办法
Jboss AS 7 在部署web应用程序的时候无法初始化spring的bean(在tomcat下边不会出现这个问题) 原因是web应用程序没有导入jboss对spring的支持的jar包 解决方法: ...
- highmaps如何自定义 区间的颜色刻度
https://api.highcharts.com/highmaps/colorAxis.dataClassColor http://jsfiddle.net/gh/get/library/pure ...
- #cat ora11g_ora_.trc
Trace file /u02/app/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_31212.trc Oracle Database 11g Enterpri ...
- Java多线程(4)----线程的四种状态
与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. 1 // 开始 ...
- linux启动
启动顺序:POST加电自检——加载BIOS——读取MBR——GRUB引导——加载kernel——rc0~rc6级别启动——加载内核模块——加载rc.sysinit——inittab运行级别——读取rc ...
- Tfs 2015 代理池配置笔记
Tfs的构建代理池其实是在代理服务器上开启一个TFSBuild的代理服务,配好相关的Tfs地址后,就能在Tfs管理界面看到了. 如果是Tfs服务和发布代理是同一台服务器,具体操作详见: 安装TFS20 ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- MongoDB Windows环境安装及配置( 一)
原文http://www.cnblogs.com/lzrabbit/p/3682510.html MongoDB一般安装 1.首先到官网 (http://www.mongodb.org/downloa ...
- SQL基础--查询之五--查询语句一般格式
SQL基础--查询之五--查询语句一般格式