题目传送门

题意:现在有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的更多相关文章

  1. 【CF1082F】Speed Dial(动态规划)

    [CF1082F]Speed Dial(动态规划) 题面 CF 洛谷 题解 把\(Trie\)树建出来之后发现就是一个树型\(dp\),每个点会对于其父亲中第一个被标记的点产生贡献. 那么把第一个点压 ...

  2. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  3. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  4. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  5. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  6. 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 ...

  7. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  8. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

  9. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

随机推荐

  1. 消息中间件-activemq实战之消息持久化(六)

    对于activemq消息的持久化我们在第二节的时候就简单介绍过,今天我们详细的来分析一下activemq的持久化过程以及持久化插件.在生产环境中为确保消息的可靠性,我们肯定的面临持久化消息的问题,今天 ...

  2. LK的NOIP膜拟赛

    T1 Learn to 签到 [题目描述] 希希最喜欢二进制了.希希最喜欢的运算是\(\wedge\). 希希还喜欢很多\(01\)序列.这些序列一共有\(n\)个,每个的长度为\(m\). 希希有一 ...

  3. eclipse项目导入idea jdk版本不一致😵

    在导入eclipse项目到idea过程中遇到 Imported project refers to unkonwn jdks JavaSE-1.8 解决方法: file --> Project ...

  4. 大数据学习之旅1——HDFS版本演化

    最近开始学习大数据,发现大数据有很多很多组件,我现在负责的是HDFS(Hadoop分布式储存系统)的学习,整理了一下HDFS的版本情况.因为HDFS是Hadoop的重要组成部分,所以有关HDFS的版本 ...

  5. HTTP与HTTPS之面试必备

    本文主要讲解Http与https的区别,以及https是怎样加密来保证安全的. 首先讲这俩个协议的简单区别: HTTP:超文本传输协议. HTTPS:安全套接字层超文本传输协议HTTP+SSL HTT ...

  6. Kafka 系列(三)—— Kafka 生产者详解

    一.生产者发送消息的过程 首先介绍一下 Kafka 生产者发送消息的过程: Kafka 会将发送消息包装为 ProducerRecord 对象, ProducerRecord 对象包含了目标主题和要发 ...

  7. abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十四)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  8. 【原】iOS查找私有API

    喜接新项目往往预示的会出一堆问题.解决问题的同时往往也就是学到更多东西的时候,这也许就是学习到新东西最直接最快速的方法吧! 小编经过努力,新项目终于过测试了,可是被苹果大大给拒了,好苦啊,最近的审核真 ...

  9. Rikka with Game[技巧]----2019 杭电多校第九场:1005

      Rikka with Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Othe ...

  10. 源码分析Retrofit请求流程

    Retrofit 是 square 公司的另一款广泛流行的网络请求框架.前面的一篇文章<源码分析OKHttp执行过程>已经对 OkHttp 网络请求框架有一个大概的了解.今天同样地对 Re ...