hdoj 1074 Doing Homework 【状压dp】

来源:互联网 发布:怎么进入网站的数据库 编辑:程序博客网 时间:2024/05/22 12:02

题目链接:hdoj 1074 Doing Homework

Doing Homework

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

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个作业扣的最少分数。并输出完成作业的先后顺序,若有多种方案数输出字典序最小的方案。

思路:状压dp,我们枚举最后一个完成的作业j,那么贡献为max(剩余作业完成需要的时间last + j作业完成需要时间N[j] - 截止日期T[j], 0)。我们记录下状态的转移,然后回溯输出即可。

AC代码:

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#define CLR(a, b) memset(a, (b), sizeof(a))using namespace std;typedef long long LL;const int MAXN = 1e4 +10;const int INF = 0x3f3f3f3f;int T[15], N[15];int dp[1<<15], last[1<<15];char str[15][20];int ans[1<<15];void Solve(int s) {    if(s == 0) return ;    Solve(s ^ (1<<ans[s]));    printf("%s\n", str[ans[s]]);}int main(){    int t; scanf("%d", &t);    while(t--) {        int n; scanf("%d", &n);        for(int i = 0; i < n; i++) {            scanf("%s%d%d", str[i], &T[i], &N[i]);        }        for(int i = 0; i < (1<<n); i++) {            last[i] = 0;            for(int j = 0; j < n; j++) {                if(i & (1<<j)) {                    last[i] += N[j];                }            }        }        dp[0] = 0;        for(int i = 1; i < (1<<n); i++) {            dp[i] = INF;            for(int j = 0; j < n; j++) {                if(i & (1<<j)) {                    if(dp[i^(1<<j)] + max(0, last[i^(1<<j)] + N[j] - T[j]) < dp[i]) {                        dp[i] = dp[i^(1<<j)] + max(0, last[i^(1<<j)] + N[j] - T[j]);                        ans[i] = j;                    }                    else if(dp[i^(1<<j)] + max(0, last[i^(1<<j)] + N[j] - T[j]) == dp[i] && strcmp(str[ans[i]], str[j]) < 0) {                        ans[i] = j;                    }                }            }        }        printf("%d\n", dp[(1<<n)-1]);        Solve((1<<n)-1);    }    return 0;}
0 0