/****************************************************************************************************
狀態壓縮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;
}
HDU 1074 狀態壓縮DP
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.