您的位置:首页 > 其它

HDU——1074 Doing homework (动态规划——状态压缩)

2018-03-13 19:51 357 查看

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(n<=15)门作业,其中包括每个作业的 名称、截止日期以及需要花费的时间,如果该作业没有在截止日期内完成,那么就会扣除相应的分数。先要求我们规划一下,如何能在截止日期内完成尽可能多的作业,是的扣分最少。

思路:因为作业的数量小于等于15 ,暴力解决的话就是把每种可能顺序都列出啦,那么时间复杂度=15!,如果这样做肯定会超时。所以选择用二进制的方式来标志每种状态(状态压缩),那么时间复杂度就可以有效的减少到 2^15 (2^15 * 15) 。

所谓状态压缩就是将每种可能的情况用二进制来表示。

实例:假设有 n = 3 门功课,那么就可以用一个{000(表示没有作业写完) , 001(第三门作业写完了),010(第二门作业写完了) , 100 , 101(第一、三门作业写完了),……,111(作业全部写完了)}。

注:本题中状态转移是关键,所谓状态转移就是从当前的作业完成情况(如:001)的基础上在完成一门作业(可以是第二门,也可以是第三门),假设此时完成的是第二门,那么转移后的状态就为011。

解题步骤(以 n = 3 为例):

初始状态为 dp[000] , 依次假设完成 第一门 dp[100]、第二门 dp[010] 或者第三门 dp[001] 功课 ,同时记录各完成情况下的扣分情况。 接着 从状态 dp [001] 开始 , 在完成一门功课 (从三门功课里面选 , 实际上这里能选择的功课只有两门 , 因为不能重复做一门功课 。为解决这一问题 , 本题中 使用了 二进制的为运算符来实现 ——(i & (1 << j)) == 0 , 其中 i 表示当前状态,如 001 , j代表当前选择要去完成的科目的下标 , 当 j = 1或者2 时 ,i & (1 << j)) 的结果等于0 , 但是 当 j = 3 时 , 结果就为 1(001) 。)所以在当前的状态下可以转移到的状态可能为dp[101] 和 dp[011]。依次类推 ,其中最优解体现在 状态 dp[101] 可以从 状态 dp[001] 推得, 也可以从 dp[100]推得 , 因此 这其中有一个 比较 和 更新的 过程。

代码:

//dp[maxn] 记录当前状态下的最小扣分,其中下标表示当前的作业完成情况
//time[maxn] 记录到当前为止已经花费的时间
//post[maxn] 记录做种作业完成的顺序

#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn=(1<<15);
const int INF = 10000;
int dp[maxn];
int time[maxn];
int post[maxn];

typedef struct{
char subject[100];
int deadline;
int cost;
}Homework;

int max(int a , int b){
if(a > b)
return a;
else
return b;
}
int  main(){
int T,n,i,j,score;
Homework *work;
int pos[15];
scanf("%d",&T);
while(T--){
memset(dp , INF , sizeof(dp));
memset(time , INF , sizeof(time));
memset(post , -1 ,sizeof(post));
scanf("%d",&n);
work = new Homework
;
for(i = 0 ; i < n ; i ++)
scanf("%s %d %d",work[i].subject,&work[i].deadline,&work[i].cost);

dp[0] = 0;
time[0] = 0;

for(i = 0 ; i < (1<<n) ; i ++){
for( j = 0 ; j < n ; j ++){
if((i & (1<<j)) == 0){    //当(i & 1<<j) == 0 说明 状态 i + 1<<j 可以由状态 i 下完成 作业 j 转换
score = time[i] + work[j].cost - work[j].deadline;      //cost记录由状态 i  --- >  i + j 所需要的扣除的分数
score = max(score , 0);    //如果当前 cost 小于 0 ,说明可以在指定的 deadline 之前完成 作业j
if(score + dp[i] < dp[i + (1 << j)]){
dp[i + (1<<j)] = dp[i] + score;
time[i + (1<<j)] = time[i] + work[j].cost;
post[i + (1<<j)] = j;
}
}
}
}

j = (1<<n) - 1;
i = 0;

while(j){
pos[i] = post[j];
j = j - (1<<pos[i]);
i ++;
}

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

for(j = i - 1 ; j >= 0 ; j --)
printf("%s\n", work[pos[j]].subject);

}

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