HU-4500 Multi-bit Trie

9151 ワード

http://acm.hdu.edu.cn/showproblem.php?pid=4570
Multi-bit Trie
Time Limit:2000/1000 MS(Java/Others)    メモリLimit:32768/32768 K(Java/Others)Total Submission(s):446    Acceepted Submission(s):169
Problem Description
IP lookup is one of the key functions of routers for packet s forwarding and classitying.Generation,IP lookup can be simplified as Longest Prefix Match(LPM)proceble.That's to find the longest fidefidetwardingand then output the corese ponding Next Hop information.
HDU-4570 Multi-bit Trie
Trie-based solution is the most wildly used one to solive LPM.As shown in Fig.1(b)、an uni-b it trie is just a binary tree.Processing LPM on it needs treding it from the root to some leaf、accoding to the input packet's destination address.The longest prefix along this trversing path the matched one.In order to reduce the memory accesses for one look up,we can commpress some consecuvelyone the bite
For example、suppose the ststrides array is{3、2、1、1}、then we can trnsform the Uni-bitTre shown Fig.1(b)in to a Multii-b it Trie ShinininininFig.1(c).During the trnsforming process,somfidededededededededededededefiefiefiefishshshshshefiefieefiefiefieefiefiefiefiefiemmmfies、SoSoshshshshshshshshshshshshshefiefiefiefiefist 3.Suspspspspspspspspefi(P 2).But 110(P 5)is already exist in the FIB、so we only store the longer one 110(P 5).
Multii-bit Trie can ovioussleduce the tree level,but the problem is how to build a Mullti-bitTrie with the minimimmomomoryconsumption(the number of memomoryunits).As shwn in Fig.1,the Uni- bittrbittrtrtrtrtrtreeeinininininininininininininininininininstststststststststststststststststststststststinininininininininininininininininininininininininininininininininininininininininininininin 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 LINE s indicate the nodes in each level of the Uni-bit Trie.
Since only 64 bit of an IPv 6 address is used for forwarding、a Uni-bit Trie has maximal 64 levels.Moreover、we suppose that the stred for each level of a Multi-bit Trie must less than or equal 20.
 
 
Output
Output the minimal possible memory units consumed by the corelding Multi-bit Trie.
 
 
Sample Input
1
7
1 2 4 5 3
 
 
Sample Output
38
問題の意味が分かりにくいです
この問題は確かに意味が分かりにくいです.少なくとも私の英語のスラグにとってはそうです.他の人のブログに行ってテーマの意味を見てみました.
長さnの数列が与えられ、それをいくつかのセグメントに分けられ、HDU-4570 Multi-bit Trieが最小であることが要求され、ここでaiは各セグメントの第一項であり、biは各セグメントの長さであり、lは数列をlセグメントに分ける.
例えば、例:n=7、A={1 4 4 5 3}、それを1 2つの4つの4つの4つの4つの124 3に分けると、その空間は1*2^3+4*2^2+2+2+3+3+2=38で、1つの4つの4つの5つの124; 4つに分けると、その空間は1+2の大きな*2になります.
 
考え方:区間DP、
dp[i][j]はi-j層の最小メモリを表します.
初期条件:全圧縮または全非圧縮
圧縮は20層を超えてはいけないので、20層未満の場合は初期条件:
dp[i][j]=a[i]*fun(2,(j-i+1);
20より大きいのは圧縮しないだけです.
dp[i][j]=(sum[j]-sum[i-1]*2; 
そしてループ
dp[i][j]=min(dp[i][k]+dp[k+1][j],dp[i][j]);k:i…j
 
 
ソurce
2013 ACM-ICPC長沙競技区全国招待試合——テーマ再現
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
__int64 dp[105][105];
 __int64 a[105],sum[105];
__int64 fun(int x,__int64 y)
{
    int i;
     __int64 ans=1;
    for(i=1;i<=y;i++)
        ans=ans*x;
    return ans;
}
int main()
{
    int i,t,n,j,k,g;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
         memset(sum,0,sizeof(sum));
         memset(a,0,sizeof(a));
         scanf("%d",&n);
       for(i=0;i<n;i++)
         {
             scanf("%I64d",&a[i]);
        }
         sum[0]=a[0];
         for(i=1;i<n;i++)
             sum[i]=sum[i-1]+a[i];

        for(k=0;k<n;k++)
        {
           for(i=0;i<n-k;i++)
            {
                   j=i+k;
                   if(k<20)//    20 ,   
                     dp[i][j]=a[i]*fun(2,(j-i+1));
                   else
                      dp[i][j]=(sum[j]-sum[i-1])*2;//    20,    
               for(g=i;g<=j;g++)
               {

                   dp[i][j]=min(dp[i][j],dp[i][g]+dp[g+1][j]);


               }
            }
        }
        
       printf("%I64d
",dp[0][n-1]); } return 0; } /* 1 7 1 2 4 4 5 4 3 */