【CF1082F】Speed Dial(动态规划)

题面

CF

洛谷

题解

把\(Trie\)树建出来之后发现就是一个树型\(dp\),每个点会对于其父亲中第一个被标记的点产生贡献。

那么把第一个点压入状态。

设\(f[i][p][k]\)表示当前点\(i\),其到根的链上第一个被标记的点是\(p\),其子树内总共选了\(k\)个点的最小代价。

枚举儿子是选还是不选进行转移。

时间复杂度\(O(n^2k^2)\)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 505
const int inf=1073741823;
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
struct Node{int son[10],v;}t[MAX];
int tot=1;
int n,K,ans=inf;char ch[MAX];
int f[MAX][MAX][12],dep[MAX];
void Insert(char *s,int w)
{
int u=1,l=strlen(s+1);
for(int i=1;i<=l;++i)
{
if(!t[u].son[s[i]-48])t[u].son[s[i]-48]=++tot;
u=t[u].son[s[i]-48];dep[u]=i;
}
t[u].v+=w;
}
void cmin(int &x,int y){x=min(x,y);}
int Solve(int u,int ld,int p)
{
if(~f[u][ld][p])return f[u][ld][p];
int g[12];memset(g,63,sizeof(g));
g[u==ld]=t[u].v*(dep[u]-dep[ld]);
for(int i=0;i<=9;++i)
{
int v=t[u].son[i];if(!v)continue;
int tmp[12];memset(tmp,63,sizeof(tmp));
for(int j=0;j<=K;++j)
for(int k=0;k+j<=K;++k)
cmin(tmp[j+k],g[j]+min(Solve(v,ld,k),k?Solve(v,v,k):inf));
for(int j=0;j<=K;++j)g[j]=tmp[j];
}
for(int i=0;i<=K;++i)f[u][ld][p]=g[p];
return f[u][ld][p];
}
int main()
{
n=read();K=read()+1;memset(f,-1,sizeof(f));
if(n<K){puts("0");return 0;}
for(int i=1;i<=n;++i)scanf("%s",ch+1),Insert(ch,read());
for(int i=0;i<=K;++i)ans=min(ans,Solve(1,1,i));
printf("%d\n",ans);
return 0;
}

【CF1082F】Speed Dial(动态规划)的更多相关文章

  1. HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. how to export chrome speed dial extension?

    locate chrome-extension_dgpdioedihjhncjafcpgbbjdpbbkikmi_0.localstorage, copy it to you want, everyt ...

  3. CodeForces 1082 F Speed Dial

    题目传送门 题意:现在有n个电话号码,每个电话号码为si,拨打次数为pi. 现在有k 个快捷键,每次拨打号码之前可以先按一次快捷键,然后再输入数字,现在问输入数字次数是多少.快捷键的号码可以不在电话簿 ...

  4. CF1082解题报告

    CF1082A Vasya and Book 模拟一下即可 \(Code\ Below:\) #include <bits/stdc++.h> using namespace std; c ...

  5. Chrome 及其 插件“个性化设置”备份

    Chrome版本发布时间表 2016.10.13 v54.0.2840.59  主题颜色由 蓝色 变为 灰色 2016.11.17 重新使用 Chrome 浏览器(v54.0.2840.99),并设置 ...

  6. 常用的Firefox浏览器插件、Chrome浏览器插件收藏

    [血的教训] 不要去下载“Firefox中国版(谋智网络)”,默认情况下会给你安装好多的莫名其妙的插件,推荐去Firefox官方下载原版. Firefox 原版官方网址: https://www.mo ...

  7. Windows And Video Memory

    MSDN Blogs > Zemblanity > Windows And Video Memory   Windows And Video Memory Tom_Mulcahy 11 F ...

  8. CentOS6.5 安装Sphinx 配置MySQL数据源

      前提安装完mysql,并创建测试表和数据 DROP TABLE IF EXISTS `documents`; CREATE TABLE IF NOT EXISTS `documents` ( `i ...

  9. CentOS6.4 安装Sphinx 配置MySQL数据源

    前提安装完mysql,并创建测试表和数据 DROP TABLE IF EXISTS `documents`; CREATE TABLE IF NOT EXISTS `documents` ( `id` ...

随机推荐

  1. docker vm 性能优劣

    Docker容器与虚拟机区别 - unixfbi.com - 博客园 http://www.cnblogs.com/pangguoping/articles/5515286.html docker与虚 ...

  2. Requires: libc.so.6(GLIBC_2.14)(64bit)

    centos6 - CentOS 6 - libc.so.6(GLIBC_2.14)(64bit) is needed by - Server Faulthttps://serverfault.com ...

  3. 测试工具之ab

    ab命令原理  Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力.  ab命令对发出负载的计算机要求很低,既不会占 ...

  4. vue前端框架面试问题汇总

    1.active-class是哪个组件的属性?嵌套路由怎么定义?答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答: ...

  5. DTW动态时间规整

    参考: https://blog.csdn.net/raym0ndkwan/article/details/45614813

  6. 排查 Maxwell can not find database 并且使用 MySQL binlog 解决相关问题

    目前我们在使用 Maxwell 在读线上机器的 binlog 同步我们的离线数据库. 这次错误定位上,首先线要确定问题是发生在生产者 还是队列 还是消费者.经过查看各机器上任务的运行日志,定位到了问题 ...

  7. mybatis generator的maven插件,找不到properties的配置文件错误的解决

    第一次运行的时候,maven插件是正确运行了的 但后面对 maven 的 build节点做了一点修改,就开始报错,找不到 properties标签指定的的数据库连接配置文件了 修改部分如下: 这个操作 ...

  8. Delphi 限制Edit输入 多个例子

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not (key in [ '.',#8]) then ...

  9. TField OnValidate 事件

    Occurs just before the data is written to the record buffer. Write an OnValidate event handler to va ...

  10. 根据request获取请求客户端的外网ip

    //根据request获取外网ip private static String getRemoteIp(HttpServletRequest request) { //x-forwarded-for: ...