关闭

HDU1074——Doing Homework(状态压缩dp)

标签: 压缩dp算法
175人阅读 评论(0) 收藏 举报
分类:

Doing Homework

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


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
简单状态压缩dp



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

#define _ ios::sync_with_stdio(false)
typedef long long ll;
const int MAXN=20;
const int INF=1e9+7;

struct hom{
    string name;
    int d,t;
}a[MAXN];
int cmp(hom x,hom y){
    return x.name>y.name;
}

int dp[1<<20];
int sum[1<<20];
int pre[1<<20];

void dfs(int s){
    if(s==0)
        return;
    int x=pre[s];
    int temp=s&(~(1<<x));
    dfs(temp);
    cout<<a[pre[s]].name<<endl;
}
int main(){
    _;
    int T;
    cin>>T;
    while(T--){
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i].name>>a[i].d>>a[i].t;
        }
        sort(a,a+n,cmp);
        memset(sum,0,sizeof(sum));
        for(int i=0;i<1<<n;i++){
            dp[i]=INF;
            for(int j=0;j<n;j++){
                int temp=1<<j;
                if(i&temp){
                    sum[i]+=a[j].t;
                }
            }
        }
        dp[0]=0;
        for(int i=0;i<1<<n;i++){
            for(int j=0;j<n;j++){
                int temp=(1<<j);
                if(temp&i){
                    int x=i&(~temp);
                    if(sum[x]+a[j].t>a[j].d){
                        if(dp[x]+sum[x]+a[j].t-a[j].d<dp[i]){
                            pre[i]=j;
                            dp[i]=dp[x]+sum[x]+a[j].t-a[j].d;
                        }
                    }else{
                        if(dp[i]>dp[x]){
                            pre[i]=j;
                            dp[i]=dp[x];
                        }
                    }
                }
            }
        }
        int temp=((1<<n)-1);
        cout<<dp[temp]<<endl;
        dfs(temp);
    }
}









0
0
查看评论

hdu1074Doing Homework【状态压缩】

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 ...
  • zhou_yujia
  • zhou_yujia
  • 2016-01-07 09:45
  • 566

HDU1074 Doing Homework

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 deadli...
  • starcuan
  • starcuan
  • 2014-02-06 17:42
  • 957

HDU 1074 Doing Homework(状态压缩dp)

题目链接:[kuangbin带你飞]专题十二 基础DP1 D - Doing Homework题意 有n门功课需要完成,每一门功课都有时间期限以及你完成所需要的时间,如果完成的时间超出时间期限多少单位,就会被减多少学分,问以怎样的功课完成顺序,会使减掉的学分最少,有多个解时,输出功课名排列最小的...
  • to_be_better
  • to_be_better
  • 2016-01-22 17:28
  • 797

HDU - 1074 Doing Homework

题意:学生有多个任务,每个任务有规定完成的时间,还有完成的时间,超过规定一天就扣一分,求最少的扣分,相同的话,按字典序 思路:DP的状态搜索,每次枚举一个,然后从第一个开始匹配,因为题目已经排序了,所以不用考虑这个字典序最小了,对于每次新加进来的,如果超过规定时间就计算多出来的,没有的话,就是...
  • u011345136
  • u011345136
  • 2014-07-02 15:54
  • 763

HDU1074:Doing Homework(状态压缩DP)

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...
  • libin56842
  • libin56842
  • 2014-04-22 17:01
  • 6268

【ACM】D. Lettree's Math Homework

D. Lettree’s Math Homework题目描述Little lettree is a middle school student who is clever but hates doing homework very much. However, on this Qingming h...
  • linglun3
  • linglun3
  • 2015-04-11 19:21
  • 334

HDU1074 Doing Homework 状态压缩dp

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9867 ...
  • zhj_fly
  • zhj_fly
  • 2017-08-13 10:27
  • 113

hdu1074 Doing Homework(状态压缩dp)

本题数据范围较小,但暴力枚举的话肯定超时,因为最坏情况将要枚举15!种情况,因此只有采取dp。 本题有点类似于经典的数塔模型,此处用已完成的课程表示当前的状态,对于某门课程,0表示还未完成,1表示已完成,采用按位压缩的方式表示出所有的情况,因此最多有1 //状态压缩dp,类似于数塔模型。...
  • Mrhuangbo
  • Mrhuangbo
  • 2012-09-18 17:40
  • 109

HDU1074:Doing Homework(状态压缩DP)

题意: 给定每个作业的最后上交时间,完成作业所需的耗时,如果超过时间未完成每天扣一分,问怎样的顺序才能使自己扣的分最少。 第一次将状态压缩与dp相结合,写的很吐血。 #include #include #define ee 100000000 using namespace std; str...
  • qq_37325947
  • qq_37325947
  • 2017-08-17 15:00
  • 56

HDU1074——Doing Homework(状态压缩DP)

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5238...
  • tyh24689
  • tyh24689
  • 2014-08-20 23:08
  • 274
    个人资料
    • 访问:90697次
    • 积分:3369
    • 等级:
    • 排名:第11844名
    • 原创:257篇
    • 转载:6篇
    • 译文:0篇
    • 评论:8条