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 ...
随机推荐
- python 简单的实现文件内容去重
文件去重 这里主要用的是set()函数,特别地,set中的元素是无序的,并且重复元素在set中自动被过滤. 测试文本为 data.txt 具体代码如下: // 文件去重 #!/usr/bin/env ...
- Model设计中常见的技巧和注意事项
verbose_name 可以作为第一个参数传入,书写更加工整和有序: name = models.CharField('类别名',default="", max_length=3 ...
- dubbo是如何控制并发数和限流的?
ExecuteLimitFilter ExecuteLimitFilter ,在服务提供者,通过 的 "executes" 统一配置项开启: 表示每服务的每方法最大可并行执行请求数 ...
- 章节十五、6-log4 2-用默认的配置
一.实例演示 package log4jtutorial; import org.apache.logging.log4j.LogManager; import org.apache.logging. ...
- Java基础的一些知识点(一):接口interface
1.接口的含义 接口可以理解成统一的协议, 而接口中的属性也属于协议中的内容.但是接口的属性都是公共的,静态的,最终的. 接口的成员特点: 1.成员变量只能是常量,默认修饰符 public stati ...
- 世界十大OTA公司盘点
世界十大OTA公司盘点 文/刘照慧(执惠旅游联合创始人,首发百度百家) 全球在线旅游公司(OTA)经过多年发展,已经形成较为成熟的商业模式,各大巨头跑马圈地,格局初现, 这两篇文章就梳理出全球按市值( ...
- Throughput Controller
吞吐量控制器(Throughput Controller)介绍 作用:控制其子节点的执行次数与负载比例分配 Total Executions: 整个测试计划中的总执行次数 Percent Execut ...
- 上个月,我赚了2W外快。。。
前段时间和室友一起给某个公司做了一个管理系统,每个人分2W多.这里和大家分享一下做完项目后一点点感受,想到啥就说点啥. 核心竞争力 两个月就挣了2W块,挣了我爸妈两个人一年的收入,每天还贼辛苦,披星戴 ...
- 轻松pick移动开发第一篇,flex布局
一.什么是flex布局 首先提问一个问题,一般童鞋都会让子元素水平居中,那么怎么让子元素垂直居中呢?这里就要用到我们的flex布局了. 1.flex 是 flexible Box 的缩写,意为&quo ...
- k好数(动态规划)
问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22 ...