# Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9323    Accepted Submission(s): 4405

Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.

Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.

Sample Input
2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3

Sample Output
2 Computer Math English 3 Computer English Math
Hint
In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.

Author
Ignatius.L

Recommend

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

#define _ ios::sync_with_stdio(false)
typedef long long ll;
const int MAXN=20;
const int INF=1e9+7;

struct hom{
string name;
int d,t;
}a[MAXN];
int cmp(hom x,hom y){
return x.name>y.name;
}

int dp[1<<20];
int sum[1<<20];
int pre[1<<20];

void dfs(int s){
if(s==0)
return;
int x=pre[s];
int temp=s&(~(1<<x));
dfs(temp);
cout<<a[pre[s]].name<<endl;
}
int main(){
_;
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i].name>>a[i].d>>a[i].t;
}
sort(a,a+n,cmp);
memset(sum,0,sizeof(sum));
for(int i=0;i<1<<n;i++){
dp[i]=INF;
for(int j=0;j<n;j++){
int temp=1<<j;
if(i&temp){
sum[i]+=a[j].t;
}
}
}
dp[0]=0;
for(int i=0;i<1<<n;i++){
for(int j=0;j<n;j++){
int temp=(1<<j);
if(temp&i){
int x=i&(~temp);
if(sum[x]+a[j].t>a[j].d){
if(dp[x]+sum[x]+a[j].t-a[j].d<dp[i]){
pre[i]=j;
dp[i]=dp[x]+sum[x]+a[j].t-a[j].d;
}
}else{
if(dp[i]>dp[x]){
pre[i]=j;
dp[i]=dp[x];
}
}
}
}
}
int temp=((1<<n)-1);
cout<<dp[temp]<<endl;
dfs(temp);
}
}



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

2016-01-07 09:45:40

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

2014-04-22 17:01:01

#### hdu1074---Doing Homework

2015-02-12 12:53:09

#### hdu1074之状态压缩dp

2014-03-21 20:34:58

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

2013-01-22 08:26:12

#### HDU1074 Doing Homework 状态压缩DP

2015-12-06 16:56:01

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

2017-08-17 15:00:50

#### HDU1074 Doing Homework 状态压缩dp

2017-08-13 10:27:51

#### hdu1074 Doing Homework （状态压缩dp）

2012-09-19 12:06:27

#### HDU1074——Doing Homework（状态压缩DP）

2014-08-20 23:08:20