HDU 1074 Doing Homework【状压DP】

时间:2024-08-07 19:37:26

Doing Homework

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.

题意:

有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小

还是一如既往的不会做dp题,参考了大神的题解,想了很久
我的理解是状压dp,大概就是把状态用二进制数表示来节省空间?

#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define rep(i,a,n) for(int i = a; i < n; ++i)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long ll;
const ll inf = ;
const int INF =0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = ;
int t,n,m ;
int cnt,ans;
struct node{
string name;
int deadline,cost;
}cor[maxn];
struct DP{
int pre,now;
int score,time;
}dp[ << ];
int main(){
ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n;
string s;
int a,b;
for(int i = ; i < n ;i ++){
cin >> s >> a >> b;
cor[i] = node{s,a,b};
}
cnt = << n; // n门课程有cnt = 2^n种排课方案
for(int i = ; i < cnt ; i++) // i = 0 就是什么课都不选
{
dp[i].score = INF; //达到状态i所扣的分
int j = n - ; j >= ; j--)//这里不是很懂
{
int temp = << j; // 将j转化为二进制数
if(i & temp) // 状态i中选了j这门
{
int past = i - temp ; //past --- 没有选j这门的状态
int st = dp[past].time + cor[j].cost - cor[j].deadline; //看会不会超过限定日期而扣多少分
if(st < ) st = ; //因为不会加分
if(st + dp[past].score < dp[i].score)//如果扣的分比原先i状态扣的少
{
dp[i].score = st + dp[past].score;
dp[i].now = j; //以下都是记录
dp[i].pre = past;
dp[i].time = dp[past].time + cor[j].cost ;
}
}
}
}
//以下都是输出
int tmp = cnt - ;
cout << dp[tmp].score <<endl;
stack<int> st;
while(tmp){
st.push(dp[tmp].now);
tmp = dp[tmp].pre;
}
while(!st.empty()){
cout << cor[st.top()].name <<endl;
st.pop();
}
}
}