课程大作业

状压DP 课程大作业

OpenJudge - 4149:课程大作业
拿例1举例,一共有三种课程作业。
作业名称 截止时间 需要时间
Computer 3 3
English 20 1
Math 3 2
我们使用state表示完成的作业状态,001表示完成第一个作业,011表示完成第一个和第二个作业
使用cur表示当前的时间,score表示当前扣的分数。
state从1开始计算:
state cur score
001 3 0
010 1 0
计算011时,我们发现有两条路径可以实现011,
第一条 001 + 010 -> 011 ,先做第一个作业,再做第二个作业,此时
cur = [001].cur + 1 = 4 , score = [001].score + 4 - 20 = 0
第二条 010 + 001 -> 011, 先做第二个作业,再做第一个作业,此时
cur = [010].cur + 3 = 4, score = [010].score + 4 - 3 = 1
以上结果可以看出,第一个方案更加好,所以
state cur score
011 4 0
. . .
根据以上想法可以计算出111,即所有的作业都完成的情况下所扣得最少分数。

根据题意,推出状态转移方程为,
struct dpnode{
int ans = 0; //当前状态所扣得总分
int t = 0; //完成当前状态所有任务所需的总时间
string path; //路径
};

dp[state].ans = dp[state-1<<(k-1)].ans + score(k),其中score(k)的求法为 dp[state-1<<(k-1)].t + needt[k] - endt[k].
dp[state].t = dp[state-1<<(k-1)].t + needt[k]
dp[state].path = dp[state-1<<(k-1)].path + path[k]

needt[k] 为第k个任务所花费的时间
endt[k]为第k个任务的截止时间

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <limits.h>
#include <iomanip>
#include <queue>
#include <cstring>
using namespace std;

typedef long long LL;

typedef vector<int> vec;

//#pragma GCC optimize(2)

struct node{

    node(string _cname, int _endt, int _needt): cname(_cname), endt(_endt), needt(_needt) {}

    string cname;
    int endt;
    int needt;
};

struct dpnode{
    int ans = 0;
    int t = 0;
    string path;
};

static int T;
static int N;
static dpnode dp[1<<20];                              // 完成state状态的任务时所扣最小分数,以及时间

vector<node> V;

void work(void){

    //
    memset(dp, 0, sizeof (dp));

    for (int state = 1; state <= (1<<N)-1; state++) {       // 枚举所有状态
        dpnode cc;
        int flag = 0;

        for (int k = 1; k <= N; k++) {

            if(state & (1<<(k-1))){                         // 递推方程,

                if(flag == 0){
                    cc.t = dp[state-(1<<(k-1))].t + V[k].needt;
                    cc.ans = cc.t - V[k].endt;
                    cc.ans = max(0, cc.ans);
                    cc.ans = dp[state-(1<<(k-1))].ans + cc.ans;
                    cc.path = dp[state-(1<<(k-1))].path;
                    cc.path.append(1, k+'0');
                    flag = 1;
                }

                int t = dp[state-(1<<(k-1))].t + V[k].needt;
                int ans = t - V[k].endt;
                ans = max(ans, 0);
                ans = dp[state-(1<<(k-1))].ans + ans;
                string path = dp[state-(1<<(k-1))].path;
                path.append(1, k+'0');

                if(ans < cc.ans){
                    cc.t = t;
                    cc.ans = ans;
                    cc.path = path;
                }else if (ans == cc.ans) {      //保证字典序
                    if(path < cc.path){
                        cc.t = t;
                        cc.ans = ans;
                        cc.path = path;
                    }
                }
            }
        }
        dp[state].t = cc.t;
        dp[state].ans = cc.ans;
        dp[state].path = cc.path;
    }

    cout << dp[(1<<N)-1].ans << endl;

    for (char cc : dp[(1<<N)-1].path) {
        cout << V[cc-'0'].cname << endl;
    }
}



int main()
{

    //freopen("E:\\Desktop\\data.txt", "r", stdin);
    //ios::sync_with_stdio(false);

    cin >> T;
    node cc("", 0, 0);

    while (T--) {
        cin >> N;

        V.clear();
        V.push_back(cc);

        for (int i = 0; i < N; i++) {
            cin >> cc.cname >> cc.endt >> cc.needt;
            V.push_back(cc);
        }
        work();
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值