CodeForces 1082 F Speed Dial
题意:现在有n个电话号码,每个电话号码为si,拨打次数为pi。 现在有k 个快捷键,每次拨打号码之前可以先按一次快捷键,然后再输入数字,现在问输入数字次数是多少。快捷键的号码可以不在电话簿上。
题解:
先构建一个字典树,然后在字典树上进行DP。
dp[x][rem][fa] x -> 节点x rem -> 还有rem次快捷键次数 fa 最近的那个父亲用了这个快捷键
dp2[x][rem][fa][i] 前面和上面一样 i代表的是处理到i这个节点的最优花费。
dp[x][rem][fa]为可以包含x的最优花费。
dp2[x][rem][fa][i]为不可以包含x的最优花费。
转移方式:
1. 当rem > 1的时候 我们可以用 dp[x][rem-1][x] 转移到 dp[x][rem][fa]
2. 对于 dp2[x][rem][fa][i] 我们可以用 dp2[x][rem-j][fa][i+1] + dp[ch[i]][j][fa] 转移过来。
3. 对于 dp[x][rem][fa][i] 我们可以用 dp[2][x][rem][0] + (deep[x] - deep[fa]) * cnt[x] 转移过来。
简单的来说, 就是对以x为根的树来说, 我们可以用子树上的状态转移过来。
需要注意的有2个点:
1 记忆化转移, 因为会有很多的点的状态是重复的。
2 先把小次数的算出来, 因为在大次数的转移的过程中需要用的小次数的值。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
struct Node{
int son[];
int deep;
int cnt;
Node(){
memset(son, -, sizeof(son));
cnt = deep = ;
}
}Trie[];
int dp[][][];
int dp2[][][][];
char s[N];
int tot = ;
void add(){
int now = , cnt;
scanf("%s%d", s, &cnt);
int len = strlen(s);
for(int i = ; i < len; ++i){
int to = s[i] - '';
if(Trie[now].son[to] == -){
Trie[now].son[to] = ++tot;
Trie[tot].deep = Trie[now].deep + ;
}
now = Trie[now].son[to];
}
Trie[now].cnt += cnt;
}
int dfs(int x, int rem, int fa){
if(dp[x][rem][fa] != -) return dp[x][rem][fa];
dp[x][rem][fa] = inf;
if(rem) dp[x][rem][fa] = min(dfs(x, rem-, x), dp[x][rem][fa]);
vector<int> ch;
for(int i = ; i < ; ++i)
if(Trie[x].son[i] != -)
ch.pb(Trie[x].son[i]);
dp2[x][rem][fa][ch.size()] = ;
for(int i = int(ch.size())-; i >= ; --i){
for(int j = ; j <= rem; ++j){
dp2[x][rem][fa][i] = min(dp2[x][rem][fa][i], dp2[x][rem-j][fa][i+] + dfs(ch[i], j, fa));
}
}
dp[x][rem][fa] = min(dp[x][rem][fa], dp2[x][rem][fa][]+Trie[x].cnt*(Trie[x].deep - Trie[fa].deep));
return dp[x][rem][fa];
}
int main(){
int n, k;
scanf("%d%d", &n, &k);
for(int i = , v; i <= n; ++i)
add();
memset(dp, -, sizeof(dp));
memset(dp2, inf, sizeof(dp2));
int ans = dfs(,k,);
cout << ans << endl;
return ;
}
CodeForces 1082 F Speed Dial的更多相关文章
- 【CF1082F】Speed Dial(动态规划)
[CF1082F]Speed Dial(动态规划) 题面 CF 洛谷 题解 把\(Trie\)树建出来之后发现就是一个树型\(dp\),每个点会对于其父亲中第一个被标记的点产生贡献. 那么把第一个点压 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- Codeforces 622 F. The Sum of the k-th Powers
\(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
随机推荐
- SpringBoot Jar包瘦身 - 跟大文件说再见!
前言 SpringBoot部署起来配置非常少,如果服务器部署在公司内网,上传速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼.就是 编译出来的 Jar 包很大,如果工程引入了许多开 ...
- python3 编译安装
前言: Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7版本,默认的python被系统很多程序所依赖,比如centos下的yum就是python ...
- 夯实Java基础(十)——抽象类和接口
转载自:http://cmsblogs.com/ 该博主的网站上干货非常!非常!非常多(说三遍),强烈推荐大家前去学习. 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法 抽象类与接口是 ...
- 准时制生产(Just in Time,JIT)
准时制生产(Just in Time,JIT)称为及时生产,出自日本丰田. 1.JIT生产方式的管理理念 JIT的基本概念事指在所需要的精确时间内,按所需要的质量和数量,生产所 ...
- FTP服务端部署
FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...
- bat 搜索进程名并kill
@echo off set/p "target=进程名(默认nginx): "if not defined target (set "target=nginx" ...
- react解析: render的FiberRoot(三)
react解析: render的FiberRoot(三) 感谢 yck: 剖析 React 源码解析,本篇文章是在读完他的文章的基础上,将他的文章进行拆解和加工,加入我自己的一下理解和例子,便于大家理 ...
- DedeCMS V5.7 SP2前台文件上传漏洞(CVE-2018-20129)
DedeCMS V5.7 SP2前台文件上传漏洞(CVE-2018-20129) 一.漏洞描述 织梦内容管理系统(Dedecms)是一款PHP开源网站管理系统.Dedecms V5.7 SP2版本中的 ...
- 精通Android4.0开发视频【张泽华】-完整版下载
观看须知: 本视频教程为黑马程序员 张泽华老师历经2年时间整理 适合有JavaWeb基础同学学习,教程采用的AVI方式发布,所以看起来很流畅. 视频概括: 1. 本套视频不同于市面上任何一套andro ...
- 《机器学习技法》---GBDT
1 对决策树使用adaboost 对决策树使用adaboost时,有以下几个问题: (1)adaboost每次更新的样本权重如何应用到决策树中? 由于我们不知道决策树的err目标是什么,因此通常的方法 ...