题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18)

分析:定义dp[status][last],status为每道菜的状态,1为吃,0为不吃,last为最后一个菜,dp[status][last],在status状态下最后吃last的最大兴奋值,然后枚举status去更新后面的状态,详细见代码

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,k;
ll w[20],ma[20][20],dp[(1<<18)+10][20],ans=0;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>k;
for(int i=0;i<n;i++)
cin>>w[i];
for(int i=1;i<=k;i++)
{
int a,b;
ll c;
cin>>a>>b>>c;
ma[a-1][b-1]=c;
}
for(int i=0;i<n;i++)
dp[(1<<i)][i]=w[i];
for(int i=0;i<(1<<n);i++)
{
int cnt=0;
for(int j=0;j<n;j++)//当前状态可以转移到“当前状态+一个菜”
{
if((i&(1<<j))==0)continue;
cnt++;
// cout<<cnt<<endl;
for(int k=0;k<n;k++)//找到一个当前状态不吃的菜,用当前状态去更新吃第k个菜的状态
{
if((i&(1<<k))==1)continue;
dp[(i^(1<<k))][k]=max(dp[(i^(1<<k))][k],dp[i][j]+w[k]+ma[j][k]);
}
}
if(cnt==m)
for(int j=0;j<n;j++)ans=max(ans,dp[i][j]);
}
cout<<ans<<endl;
return 0;
}

  

codeforces#580 D. Kefa and Dishes(状压dp)的更多相关文章

  1. 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 ...

  2. Codeforces ----- Kefa and Dishes [状压dp]

    题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...

  3. CF580D Kefa and Dishes 状压dp

    When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. Th ...

  4. codeforces 580D Kefa and Dishes(状压dp)

    题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值.问如果吃m个菜,最大价值是多大.其中n<=18 思路:一看n这么小 ...

  5. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  6. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  7. Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)

    题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...

  8. Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa

    原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...

  9. D. Kefa and Dishes(状压)

    永久打开的传送门 \(这次总算没有写砸........\) \(设f[i][j]为上一次吃的i物品状态为j的最大收益\) \(那么我们就暴力枚举所有状态i,然后在当前状态找出一个没吃的食物j,再去找一 ...

随机推荐

  1. 利用dockerfile制作基于centos7的lnmp镜像(亲测,详细版)

    首先呢,这篇文章,也是小弟参考了许多文章,自己整理出来的,有很多不足之处还有待加强,期待各位评论. > LNMP 是代表 Linux 系统下的 Nginx.Mariadb.PHP 相结合而构建成 ...

  2. List泛型与DataTable相互转换

    public static class ExtensionMethods{/// <summary>/// 将List转换成DataTable/// </summary>/// ...

  3. C#判断文件编码——常用字法

    使用中文写文章,当篇幅超过一定程度,必然会使用到诸如:“的”.“你”.“我”这样的常用字.本类思想便是提取中文最常用的一百个字,使用中文世界常用编码(主要有GBK.GB2312.GB18030.UTF ...

  4. 通过Socket让远程电脑执行脚本

    实现功能: 客户端发送命令,服务器接收命令并执行 服务端: import socketserver, os class MyTCPHandler(socketserver.BaseRequestHan ...

  5. Jenkins2.32打包Unity项目的记录

    前言 使用jenkins来打包unity3d的工程. jenkins :2.50 /2.32.3(长期支持版 建议使用此版本) 操作系统:windows 7 x64 sp1 (打包安卓和win) ,m ...

  6. Win10改AHCI无需重装系统(无需改注册表)的方法

    下面就开始:1.开机后按下WIN键 加 R键2.输入 msconfig3.如图中所示进行点击.1 引导界面 2安全引导打钩 .最小打钩 3 下面的确定.4.点击重新启动5.在重启时连续按 F2 进入B ...

  7. jQuery -- 光阴似箭(五):AJAX 方法

    jQuery -- 知识点回顾篇(五):AJAX 方法 1. $.ajax 方法:用于执行 AJAX(异步 HTTP)请求. <!DOCTYPE html> <html> &l ...

  8. 附2 rabbitmq用户管理、角色管理与权限管理

    本文摘自:http://my.oschina.net/hncscwc/blog/262246 1. 用户管理 用户管理包括增加用户,删除用户,查看用户列表,修改用户密码. 相应的命令 (1) 新增一个 ...

  9. UDP Health Checks

    This chapter describes how to configure different types of health checks for UDP servers in a load-b ...

  10. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...