HDU 1074 状态压缩DP
2011-11-18 23:21
375 查看
/**************************************************************************************************** 状态压缩DP,乱搞搞出来的。。。用二进制位压缩状态,例如101表示第2,0个作业已经做了,第1个作业还没做的状态,那么 显然应该从100或者001推101,那么就用个&啊,^啊,算啊。。。然后就是裸DP了,记得记录路径就是了 ****************************************************************************************************/ #include <iostream> #include <iomanip> #include <functional> #include <algorithm> #include <complex> #include <cstdlib> #include <cstring> #include <fstream> #include <sstream> #include <utility> #include <bitset> #include <cctype> #include <cstdio> #include <limits> #include <memory> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <list> #include <map> #include <set> using namespace std; #define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) ) #define CLR(x, k) memset((x), (k), sizeof(x)) #define CPY(t, s) memcpy((t), (s), sizeof(s)) #define SC(t, s) static_cast<t>(s) #define LEN(s) static_cast<int>( strlen((s)) ) #define SZ(s) static_cast<int>( (s).size() ) typedef double LF; //typedef long long LL; //GNU C++ //typedef unsigned long long ULL; typedef __int64 LL; //Visual C++ 2010 typedef unsigned __int64 ULL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; typedef map<int, int>::iterator MI; typedef vector<int>::iterator VI; typedef list<int>::iterator LI; typedef set<int>::iterator SI; template <typename T> T sqa(const T &x) { return x * x; } template <typename T> T gcd(T a, T b) { if (!a || !b) { return max(a, b); } T t; while (t = a % b) { a = b; b = t; } return b; } template <typename T> T ext_gcd(T a, T b, T &x, T &y) { if (!b) { x = 1; y = 0; return a; } T d = ext_gcd(b, a % b, x, y); T t = x; x = y; y = t - a / b * y; return d; } template <typename T> T invmod(T a, T p) { LL inv, y; ext_gcd(a, p, inv, y); inv < 0 ? inv += p : 0; return inv; } const int INF_INT = 0x3f3f3f3f; const LL INF_LL = 0x7fffffffffffffffLL; //15f const double oo = 10e9; const double eps = 10e-7; const double PI = acos(-1.0); #define ONLINE_JUDGE const int MAXN = 16; int test, n; struct Node { string name; int d, c; }nd[MAXN]; struct DP { int val; int day; int pre, id; }dp[1 << MAXN]; void ace() { int cap; int stop, S[MAXN]; for (cin >> test; test--; ) { cin >> n; cap = (1 << n); for (int i = 0; i < n; ++i) { cin >> nd[i].name >> nd[i].d >> nd[i].c; } for (int i = 0; i < cap; ++i) { dp[i].pre = -1; dp[i].day = INF_INT; dp[i].val = INF_INT; dp[i].id = -1; } dp[0].val = dp[0].day = 0; for (int ind = 1; ind < cap; ++ind) { for (int i = n - 1; i >= 0; --i) { int ptr = (1 << i); if (ptr & ind) { int tmp = (ind ^ ptr); int buf = dp[tmp].day + nd[i].c; int token = 0; if (buf > nd[i].d) { token = buf - nd[i].d; } if (dp[ind].val > dp[tmp].val + token || dp[ind].val == dp[tmp].val + token && dp[ind].day > dp[tmp].day + nd[i].c) { dp[ind].val = dp[tmp].val + token; dp[ind].day = dp[tmp].day + nd[i].c; dp[ind].id = i; dp[ind].pre = tmp; } } } } stop = 0; int root = cap - 1; cout << dp[root].val << endl; while (root > 0) { S[stop++] = dp[root].id; root = dp[root].pre; } for (int i = stop - 1; i >= 0; --i) { cout << nd[ S[i] ].name << endl; } } return ; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ace(); return 0; }
相关文章推荐
- HDU1074——Doing Homework(状态压缩dp)
- HDU 1074 Doing Homework (二进制状态压缩,状压dp)
- hdu1074 Doing Homework(状态压缩DP Y=Y)
- hdu1074 状态压缩dp 记录路径
- HDU 1074 (状态压缩DP)
- hdu 1074 Doing Homework 状态压缩的DP
- HDU-1074 Doing Homework 状态压缩DP
- hdu1074 状态压缩dp 记录路径
- HDU 1074 Doing Homework【状态压缩DP】
- HDU 1074【状态压缩DP】
- HDU 1074 Doing Homework 状态压缩DP
- hdu 1074 Doing Homework(状态压缩dp)
- HDU 1074 状态压缩dp
- HDU 1074 Doing Homework(DP状态压缩)
- hdu1074 状态压缩dp 记录路径
- HDU 1074 Doing Homework【状态压缩DP】
- HDU1074 Doing Homework 状态压缩dp
- hdu1074 状态压缩dp 记录路径
- HDU 1074 (DP + 状态压缩)
- HDU 1074 Doing Homework (状态压缩DP)