(HDU 1074)Doing Homework dp 状态压缩

来源:互联网 发布:千元机推荐2017知乎 编辑:程序博客网 时间:2024/04/29 12:46

Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8227 Accepted Submission(s): 3793

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.

Author
Ignatius.L

Recommend

题意:
按字典序给你n项作业的名称,截止时间,和完成所要花费的时间。作业每迟交一天扣一分,问按照什么顺序完成作业所扣的分数最小。

分析:
我们可以把所有的顺序列取出来,然后取最小的。所以就用到了二进制来表示每种状态。n<=15 所以用15位二进制来表示所有作业的的状态,第i位上 1表示作业i完成了,0表示作业i没有完成。
那么状态是怎么转移的呢?
如3位二进制111,他可以由011,101,110转移得来,而110可以由100,010转移得来。。。。我们可以列举出所有的转移关系,然后每种状态取扣分最小的路径。
实现:
我们用Node 结构体来表示每个作业的信息,
名称:str,
截止时间:want,
所需时间:need.

struct Node{    char str[109];    int want, need;}node[N];

结构体DP 来表示每种状态。
完成该种状态下所有作业后总共花费的时间:now,
完成该种状态下所有作业后总共扣的最小分数:sum,
上一个状态:next,
从上一个状态到该状态所完成的作业的编号:pos

struct DP{    int now, sum, next, pos;}dp[1<<N];

注意:
由于我们是从右到左枚举的每一位,而题目的输入保证是按字典序进行的,所以为了保证字典序最小,当后后一种情况和前一种的扣分是一样的时候,我们取后一种情况,即取=号

AC代码:

#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>#include<stack>#include<queue>using namespace std;const int N = 16;struct Node{    char str[109];    int want, need;}node[N];struct DP{    int now, sum, next, pos;}dp[1<<N];void put_ans(int x){    if(dp[x].next != -1)    {        put_ans(dp[x].next);        printf("%s\n", node[dp[x].pos].str);    }}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", node[i].str, &node[i].want, &node[i].need);        dp[0].now = dp[0].sum = 0;        dp[0].next = dp[0].pos = -1;        int m = (1<<n)-1;        for(int i=1; i<=m; i++)        {            dp[i].sum = 0x3f3f3f3f;            for(int j=0; j<n; j++)            {                if((1<<j) & i)                {                    int k = i - (1<<j);                    int v = dp[k].now + node[j].need - node[j].want;                    v = max(v, 0);                    if(dp[i].sum >= dp[k].sum+v)                    {                        dp[i].sum = dp[k].sum + v;                        dp[i].now = dp[k].now + node[j].need;                        dp[i].next = k;                        dp[i].pos = j;                    }                }            }        }        printf("%d\n", dp[m].sum);        put_ans(m);    }    return 0;}
1 0
原创粉丝点击