码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1074 Doing Homework 状压dp(第一道入门题)

时间:2018-08-29 14:38:21      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:otto   call   seve   while   cos   color   note   最小   appear   

Doing Homework

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


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
 
题意:有n门课,每门课有截止时间和完成所需的时间,
如果超过规定时间完成,每超过一天就会扣1分,
问怎样安排做作业的顺序才能使得所扣的分最小
思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况
例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,
那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,
我们看这门在现在的状态s下是不是完成,
可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,
在DP的时候要记录路径,方便之后的输出
 
#include<stdio.h>
#include<string.h>
#include<memory>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long LL;
#define INF 1<<30;
struct node
{
    string name;
    int dead,cost;
}a[50];
struct kode
{
    int time,score,pre,now;
}dp[1<<15];
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i].name>>a[i].dead>>a[i].cost;
        }
        int End=1<<n;
        for(int s=1;s<End;s++)
        {
            dp[s].score=INF;
            for(int i=n-1;i>=0;i--)//输出字典序小的方案,所以应该倒序循环当前作业
            {
                int tem=1<<i;
                if(s&tem)
                {
                    int past=s-tem;
                    int st=dp[past].time+a[i].cost-a[i].dead;
                    if(st<0)
                        st=0;
                    if(st+dp[past].score<dp[s].score)
                    {
                        dp[s].score=st+dp[past].score;
                        dp[s].now=i;
                        dp[s].pre=past;
                        dp[s].time=dp[past].time+a[i].cost;
                    }
                }
            }
        }
        stack<int> S;
        int tem=End-1;
        cout<<dp[tem].score<<endl;
        while(tem)
        {
            S.push(dp[tem].now);
            tem=dp[tem].pre;
        }
        while(!S.empty())
        {
            cout<<a[S.top()].name<<endl;
            S.pop();
        }
    }
    return 0;
}
/*

题意:有n门课,每门课有截止时间和完成所需的时间,
如果超过规定时间完成,每超过一天就会扣1分,
问怎样安排做作业的顺序才能使得所扣的分最小

思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况

例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,
那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,
我们看这门在现在的状态s下是不是完成,
可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,
在DP的时候要记录路径,方便之后的输出

*/

 

 
 

HDU 1074 Doing Homework 状压dp(第一道入门题)

标签:otto   call   seve   while   cos   color   note   最小   appear   

原文地址:https://www.cnblogs.com/yinbiao/p/9552850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!