The following code is a quick illustration of using a hash tables to keep track (and also lookup) of sub-problems in pseudo polynomial dynamic programming algorithms. You can read the problem statement here . Notice the obvious generalization of the problem (scheduling with constant number of resources) from the way I have formulated the dynamic program.
// 11/15/2014: vamsik@engr.uconn.edu// #include#include #include #include #include #include using namespace std; typedef std::pair KeyType; struct KeyTypeHasher{ inline size_t operator()(const KeyType& a) const { return (a.first)*1729 + a.second; } }; typedef std::unordered_map SubProbType; typedef typename SubProbType::iterator SubProbItr; bool IsFeasible(const std::vector & A, unsigned int G){ SubProbType sub_problems_even, sub_problems_odd; SubProbItr itr; //init// if(A[0] <= G){ sub_problems_even[KeyType(A[0], 0)] = true; sub_problems_even[KeyType(0, A[0])] = true; } for(size_t i=1; i < A.size(); i++){ SubProbType& curr_problems = (i%2) ? sub_problems_even : sub_problems_odd; SubProbType& next_problems = (i%2) ? sub_problems_odd : sub_problems_even; if(curr_problems.empty()){ return false; } next_problems.clear(); //create new sub-problems in the next level// for(itr = curr_problems.begin(); itr != curr_problems.end(); ++itr){ const KeyType &a = itr->first; if( (a.first + A[i]) <= G){ next_problems[ KeyType((a.first+A[i]), a.second)] = true; } if( (a.second + A[i]) <= G){ next_problems[ KeyType(a.first, (a.second + A[i]))] = true; } } curr_problems.clear(); } return ((A.size())%2) ? !(sub_problems_even.empty()) : !(sub_problems_odd.empty()); } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ size_t T,N; unsigned int G, a; scanf("%lu", &T); for(size_t i=0; i < T; i++){ std::vector A; scanf("%lu %u",&N, &G); for(size_t i=0; i < N; i++){ scanf("%u", &a); A.push_back(a); } printf("%s\n", IsFeasible(A, G) ? "YES" : "NO"); } return 0; }
No comments:
Post a Comment