您的位置:首页 > 其它

hdu 1074 Doing Homework

2015-05-19 01:49 211 查看


Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6050    Accepted Submission(s): 2573


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分。
问:求出完成给定的n门课的作业使得减分最少,并输出完成的顺序。
(如果完成作业的顺序有多个,则输出字典序最小的那个)

刚开始的时候没看清楚题目就想当然开始写了,结果边写边发现,根本没法写下去,我现在连当时的想法是啥都忘记了……囧
完全没有想法了,于是就停下来重新想,发现也是一点想法都没。无奈,看了看别人的题解。发现是状压dp,以前没接触过。看着
别人题解里面简单两句就解释清楚实在无力,写不出,只能看看代码自己再尝试写写。

分析:
全部作业都必须做完,而每门作业都只有两个状态,所以可以采用二进制来表示作业的状态。
比如:1 0 1 = 5,表示第一项作业和第三项作业都做完了,第二项作业没做。
所以,有n项作业的话,那就有:0~(1 << n) - 1个状态,0表示全部作业都没有做,(1 << n) - 1表示全部作业完成
用一个结构体数组去保存每个状态的几个数值:当前共花费的时间;当前状态下扣分的最小值;当前状态的前一个状态
用一个循环从0遍历到(1<<n) - 1去计算每个状态下的最优值。最后可得答案就是dp[(1<<n) - 1].reduce,然后递归输出作业完成顺序。
i表示状态的遍历,j表示作业的遍历,curtime表示新的状态:
curtime = (1 << j) | i;

curcost = dp[i].cost + hw[j].cost;
curreduce = max(curcost - hw[j].deadtime, 0) + dp[i].reduce;
dp[curtime].cost = curcost;

if(visited[curtime]) {
dp[curtime].pre = i;
dp[curtime].reduce = min(dp[curtime].reduce, curreduce) ;
}
else {

dp[curtime].pre = i;
dp[curtime].reduce =
curreduce;
4000

}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

struct HOMEWORK{
char name[110];
int deadtime;
int cost;
}hw[30];

struct NODE {
int pre;
int reduce;
int cost;
}dp[1 << 16];

int visited[1 << 16];

void output(int index) {
int curjob = (dp[index].pre ^ index) >> 1;
int curjobIndex = 0;

while (curjob) {
curjobIndex++;
curjob = curjob >> 1;
}

if (dp[index].pre != 0) {
output(dp[index].pre);
}

printf("%s\n", hw[curjobIndex].name);
}

int main() {
int cs;
int i, j, k;
int upper;

scanf("%d", &cs);

while (cs--) {
int n;

scanf("%d", &n);
upper = 1 << n;
upper = upper - 1;

for (i = 0; i < n; i++) {
scanf("%s%d%d", hw[i].name, &hw[i].deadtime, &hw[i].cost);
}

//dp[0]状态表示什么作业都没做
dp[0].cost = 0;
dp[0].pre = -1;
dp[0].reduce = 0;
memset(visited, 0, sizeof(visited));
visited[0] = 1;

for (i = 0; i <= upper; i++) {
for (j = 0; j < n; j++) {
//第j个作业的编码状态
int cur = 1 << j;

//当前作业并没有做
if (!(cur & i)) {
//算上当前作业已经完成了的新状态编码
int curtime = cur | i;
int curcost = dp[i].cost + hw[j].cost;
int curreduce = curcost - hw[j].deadtime;

//到做完当前作业(以及之前)所花的全部时间
//并没有超过当前作业的上交截止日期
if (curreduce < 0) {
curreduce = 0;
}

curreduce = curreduce + dp[i].reduce;
dp[curtime].cost = curcost;
dp[curtime].pre = i;

//若这个当前状态编码被访问过
//则需要比较当前状态编码的受处罚的分数是否比之前的要少
if (visited[curtime]) {
if (curreduce < dp[curtime].reduce) {
dp[curtime].reduce = curreduce;
}
}
else {
visited[curtime] = 1;
dp[curtime].reduce = curreduce;
}
}
}
}

printf("%d\n", dp[upper].reduce);
output(upper);
}

return 0;
}
/*
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: