# HDU 1074 Doing Homework (状压DP + 路径记录)

cost 为 完成S中的所有任务所用的总时间 - 完成v的最后期限。

#include<cstdio>
#include<iostream>
#include<stack>

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 16;

struct Nod{
char name[105];
int d,c;
}node[maxn];
int dp[1<<maxn],pre[1<<maxn];
int n;

void solve(){
int Ed = 1 << n;
fill(dp,dp+Ed,inf);
fill(pre,pre+Ed,-1);
int day,cost,S0,tmp,cnt;
dp[0] = 0;
for(int S = 1;S < Ed; S++){
for(int v = n - 1; v >= 0 ; v--){
if(!(S >> v & 1)) continue;
S0 = S - (1 << v);
day = 0;cost = 0;
for(int i = 0; i < n ; i++){
if(S0 >> i & 1) day += node[i].c;
}
if(node[v].d < day + node[v].c) cost = day + node[v].c - node[v].d;
if(dp[S] > dp[S0] + cost){
dp[S] = dp[S0] + cost;
pre[S] = S0;
//cout<<S<<" "<<S0<<" "<<v<<" "<<dp[S]<<endl;
}
}
}
printf("%d\n",dp[Ed-1]);
tmp = Ed - 1;
stack<int> stk;
while(pre[tmp] != -1){
cnt = 0;
while(!((tmp ^ pre[tmp]) >> cnt & 1)) cnt++;
stk.push(cnt);
tmp = pre[tmp];
}
while(!stk.empty()){
int top = stk.top();stk.pop();
printf("%s\n",node[top].name);
}
}

int main(){
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
for(int i = 0 ;i < n; i++){
scanf("%s%d%d",node[i].name,&node[i].d,&node[i].c);
}
solve();
}
return 0;
}


#### HDU 1074 Doing Homework(状态压缩dp)

2016-01-22 17:28:28

#### hdu_1074_Doing Homework(状压DP)

2016-05-23 15:24:53

#### 杭电ACM1074——Doing Homework~~状态压缩DP

2015-07-28 20:23:42

#### HDU - 1074 Doing Homework

2014-07-02 15:54:49

#### HDU1074：Doing Homework(状态压缩DP)

2014-04-22 17:01:01

#### hdu1074--Doing Homework（状压dp）

2015-01-10 21:30:40

#### hdu 1789 Doing Homework again 贪心算法

2013-01-22 08:26:12

#### hdu1074Doing Homework【状态压缩】

2016-01-07 09:45:40

#### hdu 状态压缩dp小集（中低难度）

2013-08-01 09:39:09

#### 状态压缩dp入门 （poj3254 Corn Fields）

2014-04-28 19:10:45