Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)
用spfa,和dp是一样的。转移只和最后一个吃的dish和吃了哪些有关。
把松弛改成变长。因为是DAG,所以一定没环。操作最多有84934656,514ms跑过,实际远远没这么多。
脑补过一下费用流,但是限制流量不能保证吃到m个菜
#include<bits/stdc++.h>
using namespace std; typedef pair<int,int> nd;
typedef long long ll;
#define fi first
#define se second
int n,m,a[];
int g[][];
const int maxs = <<;
ll d[maxs][];
bool vis[maxs][];
inline int bitcount(int s)
{
int ct = ;
while(s){
ct += s&;
s >>= ;
}
return ct;
} ll spfa()
{
queue<nd>q;
ll ans = ;
for(int i = ; i < n; i++){
q.push(nd(<<i,i));
d[<<i][i] = a[i];
vis[<<i][i] = true;
}
while(q.size()){
nd &u = q.front(); vis[u.fi][u.se] = false;
int bc = bitcount(u.fi);
if(bc == m) { ans = max(ans,d[u.fi][u.se]); q.pop(); continue; }
for(int i = ; i < n; i++){
if(&(u.fi>>i)) continue;
int ns = <<i|u.fi;
if(d[ns][i] < d[u.fi][u.se] + g[u.se][i] + a[i]){
d[ns][i] = d[u.fi][u.se] + g[u.se][i] + a[i];
if(!vis[ns][i]){
q.push(nd(ns,i)); vis[ns][i] = true;
}
}
}
q.pop();
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int k;scanf("%d%d%d",&n,&m,&k);
for(int i = ; i < n; i++){
scanf("%d",a+i);
}
while(k--){
int x,y; scanf("%d%d",&x,&y);
scanf("%d",g[x-]+y-);
}
printf("%I64d",spfa());
return ;
}
Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)的更多相关文章
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- 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 #321 (Div. 2) E Kefa and Watch (线段树维护Hash)
E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #321 (Div. 2) C Kefa and Park(深搜)
dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)
http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- Codeforces Round #651 (Div. 2) E. Binary Subsequence Rotation(dp)
题目链接:https://codeforces.com/contest/1370/problem/E 题意 给出两个长为 $n$ 的 $01$ 串 $s$ 和 $t$,每次可以选择 $s$ 的一些下标 ...
随机推荐
- redis需要掌握的知识点
- Spring入门第十二课
Bean的配置方法 通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户 ...
- 8. php回调后门
中国菜刀下载,基于原版中国菜刀优化版20160309. 下载地址: http://pan.baidu.com/s/1jHoJxHW China chopper http://pan.baidu.com ...
- json字符串与json对象之间的转换
字符串转对象(strJSON代表json字符串) var obj = eval(strJSON); (运用时候需要除了eval()以外需要json.js包) var obj = strJSON. ...
- [Lintcode]Word Squares(DFS|字符串)
题意 略 分析 0.如果直接暴力1000^5会TLE,因此考虑剪枝 1.如果当前需要插入第i个单词,其剪枝如下 1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找 – 第一个词填了ball ...
- redis win系统安装并设置开机自启
安装包下载网址 :https://github.com/MSOpenTech/redis/releases 找到最新发布的安装包进行下载 下载完后解压到c盘 打开cmd,并进到redis里面 cd C ...
- 前端面试题整理---JS基础
为了督促自己学习,整理了一下前端的面试题 JavaScript: JavaScript 中如何监测一个变量是String类型? typeof(obj)==="string"; ty ...
- Jenkins+Git+Docker+K8s部署
准备工作 Jenkins已安装 Docker和K8s部署运行成功 代码管理工具使用Git 最近公司项目使用Jenkins+Git+Docker+K8s进行持续化构建部署,这里笔者整理了一下构建部署的相 ...
- .bat 文件学习
参考文章:http://www.cnblogs.com/glaivelee/archive/2009/10/07/1578737.html 重点: @echo off 关闭回显,不显示脚本中的命令 e ...
- 搞定C系语言的的swap
http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html 原地址 Parameters, by value and by reference: Both ...