Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 0.81 KB | None | 0 0
  1. --- old.cpp 2015-01-27 11:21:37.440703996 +0100
  2. +++ new.cpp 2015-01-27 11:20:03.427223463 +0100
  3. @@ -79,8 +79,9 @@
  4.     }
  5.  }
  6.  
  7. +map<tuple<int,int,int>, int> mem;
  8.  
  9. -int recur( int val , int lastid , int root , vector < int > y )
  10. +int recur( int val , int lastid , int root , const vector<int> &y )
  11.  {
  12.     if ( val == 0 ) return 0 ;
  13.  
  14. @@ -89,14 +90,16 @@
  15.     if ( lastid  == y.size())
  16.         return ans ;
  17.  
  18. +   auto key = make_tuple(val,lastid,root);
  19. +   if (mem.find(key) != mem.end())
  20. +       return mem[key];
  21.  
  22.     for ( int  k1 = 0 ; k1 <= val ; k1++)
  23.     {
  24.         ans = min ( ans , dp[y[lastid]][k1] + recur ( val - k1 , lastid + 1 , root , y ) ) ;
  25.     }
  26.  
  27. -
  28. -   return ans ;
  29. +   return mem[key] = ans;
  30.  
  31.  }
  32.  
  33. @@ -124,6 +127,7 @@
  34.  
  35.     //case 1
  36.  
  37. +   mem.clear();
  38.     if ( present[root])
  39.     {   for ( int j = 2 ; j <= k ; j++)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement