hdu4570Multi-bit Trie (间隙DP)


Problem Description
  IP lookup is one of the key functions of routers for packets forwarding and classifying. Generally, IP lookup can be simplified as a Longest Prefix Matching (LPM) problem. That's to find the longest prefix in the Forwarding Information Base (FIB) that matches the input packet's destination address, and then output the corresponding Next Hop information.

  Trie-based solution is the most wildly used one to solve LPM. As shown in Fig.1(b), an uni-bit trie is just a binary tree. Processing LPM on it needs only traversing it from the root to some leaf, according to the input packet's destination address. The longest prefix along this traversing path is the matched one. In order to reduce the memory accesses for one lookup, we can compress some consecutively levels of the Uni-bit Trie into one level, transforming the Uni-bit Trie into a Multi-bit Trie.
  For example, suppose the strides array is {3, 2, 1, 1}, then we can transform the Uni-bit Trie shown in Fig.1(b) into a Multi-bit Trie as shown in Fig.1(c). During the transforming process, some prefixes must be expanded. Such as 11(P2), since the first stride is 3, it should be expanded to 110(P2) and 111(P2). But 110(P5) is already exist in the FIB, so we only store the longer one 110(P5).
  Multi-bit Trie can obviously reduce the tree level, but the problem is how to build a Multi-bit Trie with the minimal memory consumption (the number of memory units). As shown in Fig.1, the Uni-bit Trie has 23 nodes and consumes 46 memory units in total, while the Multi-bit Trie has 12 nodes and consumes 38 memory units in total.

Input
  The first line is an integer T, which is the number of testing cases.
  The first line of each case contains one integer L, which means the number of levels in the Uni-bit Trie.
  Following L lines indicate the nodes in each level of the Uni-bit Trie.
  Since only 64 bits of an IPv6 address is used for forwarding, a Uni-bit Trie has maximal 64 levels. Moreover, we suppose that the stride for each level of a Multi-bit Trie must be less than or equal to 20.

Output
  Output the minimal possible memory units consumed by the corresponding Multi-bit Trie.

Sample Input
1 7 1 2 4 4 5 4 3

Sample Output
38

Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现
题意:一个长度为n的数列。将其分成若干段(每一段的长度要<=20),要求∑ai*(2^bi)最小,当中ai是每一段数列的第一项,bi是每一段的长度,
bi<=20。
#include<stdio.h>
__int64 min(__int64 a,__int64 b)
{
    return a>b?

b:a; } int main() { int n,t; __int64 dp[100][100],ans[100]; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&ans[i]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp[i][j]=999999999;//注意初始化 for(int len=0;len<n;len++) for(int i=1;i<=n-len;i++) { int j=i+len; if(len<20) dp[i][j]=ans[i]*(1<<(len+1)); for(int k=i;k<j;k++) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]); } printf("%I64d\n",dp[1][n]); } }



版权声明:本文博主原创文章,博客,未经同意不得转载。

优质内容筛选与推荐>>
1、Scala进阶之路-I/O流操作之文件处理
2、Community Server专题七: Job & Timer(转载)
3、NYOJ 38
4、uc_key getshell
5、OC 初次接触


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn