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$ 的一些下标 ...
随机推荐
- UVa 11795 Mega Man's Mission (状压DP)
题意:你最初只有一个武器,你需要按照一定的顺序消灭n个机器人(n<=16).每消灭一个机器人将会得到他的武器. 每个武器只能杀死特定的机器人.问可以消灭所有机器人的顺序方案总数. 析:dp[s] ...
- HDU - 6201 transaction transaction transaction(树形dp取两点)
transaction transaction transaction Kelukin is a businessman. Every day, he travels around cities to ...
- c#---delegate关键字
http://www.cnblogs.com/wenjiang/archive/2013/03/12/2954913.html 注:只看红字 在C#中,delegate是一个神奇的关键字,值得拿出来单 ...
- python 之 序列化与反序列化、os模块
6.6 序列化与反序列化 特殊的字符串 , 只有:int / str / list / dict 最外层必须是列表或字典,如果包含字符串,必须是双引号"". 序列化:将Python ...
- SpringBoot2.0 基础案例(06):引入JdbcTemplate,和多数据源配置
一.JdbcTemplate对象 1.JdbcTemplate简介 在Spring Boot2.0框架下配置数据源和通过JdbcTemplate访问数据库的案例. SpringBoot对数据库的操作在 ...
- Ruby: Case表达式
Ruby的case表达式有两种形式: 第一种形式接近于一组连续的if语句:它让你列出一组条件,并执行第一个为真的条件表达式所对应的语句. 第二种形式,在case语句的顶部指定一个目标,而每个when从 ...
- Condition应用和源码分析
1.Condition实现一个队列public class BoundedQueue<T> { public List<T> q; //这个列表用来存队列的元素 private ...
- 基于SSM搭建网站实现增删改查
网站源码地址:https://github.com/MyCreazy/BasicOperateWebSite.git 使用maven搭建网站的时候,记得选用war包格式,有时候maven包没有引用进来 ...
- JSP 不同版本(转)
转自 http://blog.csdn.net/sunnyyoona/article/details/51076823
- HDU1296 Polynomial Problem
http://acm.hdu.edu.cn/showproblem.php?pid=1296 随手练习 #include <bits/stdc++.h> using namespace s ...