【HDU4570】【Multi-bit Trie】

来源:互联网 发布:苹果手机java编程软件 编辑:程序博客网 时间:2024/06/07 02:59

Multi-bit Trie

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 543    Accepted Submission(s): 217


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
171244543
 

Sample Output
38
 

Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现
 

Recommend
zhoujiaqi2010



 

题目意思:

题目难的不是算法难的是英语呀。。。。TT。。

转化题意,就是给n个数,求一个划分使得各段
num[i]*2^len之和最小。num[i]为该段的第一个数。len为该段的长度。
每一段的个数不超过20。(开始没读出来。wa了千百回)
思路:
dp[i]表示i个数时满足题目要求的划分的最小总和。

dp[i]=Min(dp[i],dp[i-j]+num[i-j+1]*2^j);


#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;ll T;ll n;ll a[70];ll dp[70];int main(){    scanf("%I64d",&T);while(T --){scanf("%I64d",&n);cout << n << endl;for(ll i=1;i<=n;i++){scanf("%I64d",&a[i]);cout << a[i] << endl;}memset(dp,0,sizeof(dp));for(ll j=1;j<=n;j++){for(ll l=1;j- l +1 >= 1 && l <= 20;l++) {ll i = j - l + 1 ;if(dp[j] == 0) dp[j] = dp[i-1] + a[i] * (1LL << l);dp[j] = min(dp[j],dp[i-1] + a[i] * (1LL << l)) ;     }}printf("%I64d\n",dp[n]);cout << endl;}    return 0;}


0 0
原创粉丝点击