Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)
2024-08-23 08:17:57
用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 ;
}
最新文章
- [Python] python vs cplusplus
- MATLAB plot函数的一些参数
- string.Join和Reverse的简单使用示例
- photoSlider-html5原生js移动开发轮播图-相册滑动插件
- CloseHandle(),TerminateThread(),ExitThread()的区别
- tolower (Function)
- 基于 Aspose.Cells与XML导入excel 数据----操作类封装
- Linux查看监听端口的脚本测试
- 问题记录2019-03-06(todo)
- 动态规划——Burst Ballons
- Clustering[Spectral Clustering]
- SpringBoot定时任务说明
- 2019年度【计算机视觉&;机器学习&;人工智能】国际重要会议汇总
- 浏览器执行代码 是jsp 服务端执行的是<;%%>;
- 人生苦短:Python里的17个“超赞操作
- 后台返回路由的数组,然后根事先写好的路由比对如果相等就放到一个数组中https://www.cnblogs.com/zhengrunlin/p/8981017.html
- 16进制转ascii,转字符串
- html + css + jquery实现简单的进度条实例
- [转载]《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化
- IOS开发之自定义键盘
热门文章
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
- imgAreaSelect插件
- Python实现二叉树的前序、中序、后序、层次遍历
- argparse 在深度学习中的应用
- JSPs only permit GET POST or HEAD的解决方案(REST风格)
- Django之缓存+序列化+信号+ORM性能优化+验证码
- 【手撸一个ORM】第五步、Expression(表达式目录树)转换为Where子句
- Net Core IIS Express In
- F. Clique in the Divisibility Graph DP
- Jenkins+Gitlab+Ansible自动化部署(二)