HDU 1074 Doing Homework(状压DP)

本文介绍了一个通过状态压缩枚举的方法来解决学生如何合理安排作业提交顺序的问题,以最小化因逾期而被扣除的成绩分数。该方法适用于最多15门课程的情况,并提供了一种高效的算法实现方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Doing Homework

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


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门作业,每一门有完成所需时间和截止日期,对于每一门,超过截止时间一天,就扣一分,问怎么安排才能使完成所有课程扣分最少

思路:15门课,可以考虑状态压缩来枚举,en=1<<15 将每种课程的组合方式枚举一下,相与等于1表示当前课程没有完成,可以考虑是否在当前组合下完成它

需要注意一下输出技巧,题目中很好的一点是 Hint 已经说了给的数据都按字典序排列好了

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#define INF 1<<25
using namespace std;

struct Node
{
    string na;
    int ded,last;
}f[200];

struct knode
{
    int now,pre,cost,time; //指向当前,指向上一个状态,花费,时间
}dp[1<<15+1];

int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            cin>>f[i].na>>f[i].ded>>f[i].last;
        }
        memset(dp,0,sizeof dp);

        int en =1<<n;
        for(int s=1;s<en;s++)
        {
            dp[s].cost=INF;
            for(int i=n-1;i>=0;i--)   //逆向枚举是为了方便后面按字典序输出
            {
                int tmp=1<<i;
                if(s&tmp)
                {
                    int last=s-tmp;
                    int ff=dp[last].time+f[i].last-f[i].ded;
                    if(ff<0)
                        ff=0;
                    if(dp[last].cost+ff<dp[s].cost)
                    {
                        dp[s].cost=dp[last].cost+ff;
                        dp[s].now=i;
                        dp[s].pre=last;
                        dp[s].time=dp[last].time+f[i].last;
                    }
                }
            }
        }

        stack<int>st;
        int tmp=en-1;
        cout<<dp[tmp].cost<<endl;

        while(tmp)  //从后指向前,逆向入栈,后面出栈就能变成字典序
        {
            st.push(dp[tmp].now);
            tmp=dp[tmp].pre;
        }
        while(!st.empty())
        {
            cout<<f[st.top()].na<<endl;
            st.pop();

        }

    }

    return 0;
}

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值