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 ...
随机推荐
- 动态开内存(malloc与calloc)
malloc与calloc 1.函数原型 #include<stdlib.h> void *malloc(unsigned int size); //申请size字节的内存 voi ...
- Python基础总结之初步认识---class类的继承(下)。第十五天开始(新手可相互督促
年薪百万的步伐慢了两天hhhh严格意义是三天.最近买了新的玩具,在家玩玩玩!~~~~ 今天开始正式认识类的继承.类的继承是怎么继承呢?看下代码: class Animal(object): #父类 d ...
- 关于p标签不能嵌套div标签引发的标签嵌套问题总结
问题由来:<p>中嵌套<div>标签,两个都是块级元素,按理应该可以正常显示,但是最后的结果居然是多出来一段<p>的效果,所以就在网上找了许多关于标签嵌套规则的资料 ...
- Spring Boot 2.X 如何快速整合jpa?
本文目录 一.JPA介绍二.Spring Data JPA类结构图1.类的结构关系图三.代码实现1.添加对应的Starter2.添加连接数据库的配置3.主要代码 一.JPA介绍 JPA是Java Pe ...
- 使用PIP键盘输入数字小数位--Smart LCD
应用范例: 使用TOPWAY Smart LCD (HMT050CC-C) 使用PIP键盘输入数字小数位 第一步 建立工程 第二步 建立三个页面,导入图片 点击工作区域, 右面显示页面属性 属性中Ba ...
- Gin + Vue全栈开发实战(二)
尝试地写了第一篇自己学习Go Web框架的感受和入门的文章,发现反响还不错,大家也提出了很多的问题来一起交流.近期也渐渐地出现了很多有关go语言开发的相关文章,包括有在蚂蚁金服的大牛的分享,我也一直有 ...
- net core Webapi基础工程搭建(二)——创建工程
目录 前言 创建工程 工程文件概述(个人理解) 运行 小结 前言 前面介绍了开发工具及net core版本,后端工程的框架结构开发工具及环境,因为是基础工程,所以没考虑太复杂的框架,如果有架构师请勿喷 ...
- ASP.NET Core 2.2 : 二十六. 应用JWT进行用户认证
本文将通过实际的例子来演示如何在ASP.NET Core中应用JWT进行用户认证以及Token的刷新方案(ASP.NET Core 系列目录) 一.什么是JWT? JWT(json web token ...
- oauth2.0授权详解
学习oauth认证之前先回顾一下通过sessionid的会话过程 关于session与cookie的请戳:https://www.cnblogs.com/moran1992/p/10793748.ht ...
- k8s西游记 - 切换网络插件IP池
前言 最近在另一个k8s集群中,搭建了kong网关,在配置OIDC插件时,希望使用Memcahe代替Cookie来存储会话信息,于是把部署在同一局域网Memcahe的内网IP,比如:192.168.1 ...