hdu 1074 doing homework

一道状态压缩的dp题。我是第一次接触这类题型,说实话,我是完全摸不着边。大神的解题报告我也看不懂,一行代码一行代码的响了很久,终于弄清了点头绪。
dp[i]:到第i种情况,最小扣分数。
我们用状态压缩,比如,二进制100代表完成了第三门课程。即dp[4]表示只完成第三门课程的最少扣分数。dp[7]表示完成第一,二,三,三门课程的最小扣分。
本题还加入了路径记录。可以创造一个结构体。

struct DPT
{
    int time;
    int score;
    int past; //上一门完成的功课的位置
    int pos; //现在的位置
    DPT()
    {
        time=pos=past=score=0;
    }
}dp[1<<15];

比如:dp[5].pos表示到底完成了那门课,使得从上一种情况变为这种情况,past记录上一种情况的位置。
代码很大一部份都抄袭了大神们的代码,很抱歉,也很感谢他们的分享。谢谢!

#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
using namespace std;
const int INF=1e9;
struct node
{
    string subject;
    int deadline,time;
}a[16];
struct DPT
{
    int time;
    int score;
    int past; //上一门完成的功课的位置
    int pos; //现在的位置
    DPT()
    {
        time=pos=past=score=0;
    }
}dp[1<<15];
int main()
{
    //freopen ("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int end=1<<n;
        for(int i=0;i<=n-1;i++)
        {
            cin>>a[i].subject>>a[i].deadline>>a[i].time;
        }
        for(int i=1;i<end;i++)
        {
            dp[i].score=INF;
            //cout<<i<<endl;
            for(int j=n-1;j>=0;j--)
            {
                int tmp=1<<j;
                if(tmp&i)
                {
                    int past=i-tmp;
                    int reduce=dp[past].time+a[j].time-a[j].deadline;
                    //cout<<reduce<<endl;
                    if(reduce<0)    //假设没有超过截至日期,即不扣分
                        reduce=0;
                    //cout<<dp[past].score+reduce<<endl
                    if(dp[past].score+reduce<dp[i].score)
                    {
                        //cout<<a[j].subject<<endl;
                        //cout<<i<<endl;
                        dp[i].score=dp[past].score+reduce;
                        dp[i].pos=j;
                        dp[i].past=past;
                        dp[i].time=dp[past].time+a[j].time;
                        //cout<<dp[j].time<<endl;
                    }
                }
            }
        }
        stack<int> st;
        int k=(1<<n)-1;
        //cout<<k<<endl;
        cout<<dp[k].score<<endl;
        while(k!=0)
        {
            st.push(dp[k].pos);
            k=dp[k].past;
        }
        while(!st.empty())
        {
            cout<<a[st.top()].subject<<endl;
            st.pop();
        }
    }
    return 0;
}

值得好好研究的状态压缩题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值