zoukankan      html  css  js  c++  java
  • HDU 4570(区间dp)

    E - Multi-bit Trie

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     1 //2016.8.6
     2 //区间dp
     3 #include<iostream>
     4 #include<cstdio>
     5 #include<cstring>
     6 
     7 using namespace std;
     8 
     9 typedef long long ll;
    10 const int inf = 0x3f3f3f3f;
    11 ll a[70], dp[70][70];
    12 
    13 int main()
    14 {
    15     int T, n;
    16     cin>>T;
    17     while(T--)
    18     {
    19         cin>>n;
    20         for(int i = 0; i < n; i++)
    21           scanf("%lld", &a[i]);
    22 
    23         for(int i = 0; i < n; i++)
    24             for(int j = 0; j < n; j++)
    25               if(j-i<=20)
    26                 dp[i][j] = a[i]*(1<<(j-i+1));
    27               else 
    28                 dp[i][j] = inf;
    29 
    30         for(int i = 0; i < n; i++)
    31             for(int j = 0; j < n; j++)
    32             for(int k = i; k < j; k++)
    33                 dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]);    
    34 
    35         cout<<dp[0][n-1]<<endl;
    36     }
    37 
    38     return 0;
    39 }
  • 相关阅读:
    关于jstl.jar引用问题及解决方法
    React 解析/ 第二节 使用 Reac
    NOde.js的安装和简介
    JACOB调用控件函数
    Linux 常用命令
    webService接口发布失败问题
    CommonsMultipartFile---用Spring实现文件上传
    验证签名(章)是否有效的方法
    新起点,新征程
    使用C#正则表达式获取必应每日图片地址
  • 原文地址:https://www.cnblogs.com/Penn000/p/5745021.html
Copyright © 2011-2022 走看看