0
点赞
收藏
分享

微信扫一扫

Doing Homework (状态dp)


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


题目大概:

给出n门课的作业,完成的最后期限和需要花费的时间,当一门作业晚了,晚1天扣一分。问最少减几分,并且输出课的顺序。

思路:

状态dp

dp【i】表示到i状态的时候的最少的减分。


代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int ma=(1<<16);
int inf=1<<30;
int dp[ma],t[ma],qian[ma],dea[ma],hua[ma];
char s[20][110];
int n;
void outa(int x)
{
if(!x)return;
outa(x-(1<<qian[x]));
printf("%s\n",s[qian[x]]);
}

int main()
{
int m;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s%d%d",s[i],&dea[i],&hua[i]);

}

for(int i=1;i<1<<n;i++)//枚举每一个状态
{
dp[i]=inf;
for(int j=n-1;j>=0;j--)//枚举每一门课
{
int tem=1<<j;
if(i&tem)//本门课是否在状态内
{
int score=t[i-tem]+hua[j]-dea[j];//计算从没有做这门课到做这门课的分数的变化
if(score<0)score=0;
if(dp[i]>dp[i-tem]+score)//符合条件更新
{
dp[i]=dp[i-tem]+score;
t[i]=t[i-tem]+hua[j];
qian[i]=j;

}
}
}

}

int ans=1<<n;
printf("%d\n",dp[ans-1]);

outa(ans-1);//顺序输出

}

return 0;
}








举报

相关推荐

0 条评论