# Doing Homework

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

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

# 1. 分析

dp[i] = min（dp[i-(1<<j)] + cost）     j=0,1,2,3,……

# 2. AC代码

#include <iostream>
#include <cstring>
using namespace std;

#define COURSE_NUM 15
#define MAX 0x7FFF

struct Course {
int time;
char name[101];
} course[COURSE_NUM];

struct Node {
int pre;
int cost;
int cur_time;
} dp[MAX];

int main() {
int case_num;
cin >> case_num;
while (case_num--) {
memset(dp, 0, sizeof(dp));
memset(course, 0, sizeof(course));

int n;
cin >> n;
//for (int i=1; i<=n; i++)
for (int i=0; i<n; ++i)
cin >> course[i].name >> course[i].deadline >> course[i].time;

int state_num = 1<<n;
for (int i=1; i<state_num; ++i) {
for (int j=0; j<n; ++j) {
if (i&(1<<j)) {
//int pre = i&(^(1<<j));
int pre = i-(1<<j);
//int finished_time = dp[pre].cur_time + course[i].time;
int finished_time = dp[pre].cur_time + course[j].time;
//int cost = finished_time - course[i].deadline;
int cost = finished_time - course[j].deadline;
if (cost < 0) cost = 0;
cost += dp[pre].cost;
// j从零开始，表示后做序号小的课程，而题目要求相反
// if (dp[i].cur_time==0 || cost < dp[i].cost) {
if (dp[i].cur_time==0 || cost <= dp[i].cost) {
dp[i].cur_time = finished_time;
dp[i].cost = cost;
dp[i].pre = j;
}
}
}
}

cout << dp[state_num-1].cost << endl;
int a[COURSE_NUM];
int tmp = state_num-1;
int index = 0;
while(tmp) {
a[index++] = dp[tmp].pre;
tmp = tmp-(1<<dp[tmp].pre);
}
while (index>0)
cout << course[a[--index]].name << endl;
}
return 0;
}

09-04 2675

02-04 1.8万

04-17 1139

10-21 332

07-25 108

08-06 1298

03-21 4943

08-01 2832

04-28 2万