状压dp,HDU1074.Doing Homework

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

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.

2、输入输出

2.1输入

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.

2.2输出

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.

3、原题链接

Problem - 1074 (hdu.edu.cn)


二、解题报告

1、思路分析

从数据量上会往状压dp上想

我们用二进制位表示任务是否完成,那么我们最终状态是确定的

如果有n个任务,那么我们的最终状态就是(1 << n) - 1,我们记为ed

对于ed而言,代表n个任务都已经完成,它可以由n个前驱状态转移而来

假如n = 3,那么ed = 111(2),那么可以由011、101、110三个状态转移,分别代表最后完成的任务为任务1、2、3

那么对于011,101,110而言,同样可以由前驱状态转移

那么我们自顶向下进行状态转移即可

2、复杂度

时间复杂度:O(1<<N) 空间复杂度:O(1<<N)

3、代码详解

复制代码
cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
const int N = 20, inf = 0x3f3f3f3f;
struct state
{
    int pre, id, t, s;
} f[1 << N];
int cost[N], dead[N], n, tot;
string lessons[N];
void solve()
{
    cin >> n, tot = 1 << n, memset(f, 0, sizeof f);
    for (int i = 0; i < n; i++)
        cin >> lessons[i] >> dead[i] >> cost[i];
    for (int i = 1; i < tot; i++)
    {
        f[i].s = inf;
        for (int j = n - 1; j >= 0; j--)
        {
            if (i & (1 << j))
            {
                int last = i - (1 << j);
                int c = max(0, f[last].t + cost[j] - dead[j]);
                if (f[last].s + c < f[i].s)
                    f[i] = {last, j, f[last].t + cost[j], f[last].s + c};
            }
        }
    }
    cout << f[--tot].s << '\n';
    stack<int> s;
    while (f[tot].t)
    {
        s.emplace(f[tot].id), tot = f[tot].pre;
    }
    while (s.size())
        cout << lessons[s.top()] << '\n', s.pop();
}
int main()
{
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
        solve();
    return 0;
}
相关推荐
ADI_OP13 分钟前
ADAU1452的开发教程3:常规音频算法的开发(2)
算法·dsp开发·adi dsp中文资料·adi dsp·adi音频dsp·adi dsp开发教程
666HZ66615 分钟前
数据结构1.0 数据结构在学什么
数据结构·算法
君义_noip25 分钟前
信息学奥赛一本通 1951:【10NOIP普及组】导弹拦截 | 洛谷 P1158 [NOIP 2010 普及组] 导弹拦截
c++·算法·csp-j·信息学奥赛
环黄金线HHJX.25 分钟前
《QuantumTuan ⇆ QT:Qt》
人工智能·qt·算法·编辑器·量子计算
jz_ddk26 分钟前
[实战] 射频相位噪声单位转换:从dBc/Hz到rad
算法·rf·射频·相位噪声·相噪
zl_vslam28 分钟前
SLAM中的非线性优-3D图优化之地平面约束(十五)
人工智能·算法·计算机视觉·3d
空空潍30 分钟前
hot100-滑动窗口最大值(day11)
数据结构·c++·算法·leetcode
朔北之忘 Clancy34 分钟前
2025 年 6 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·算法·青少年编程·题解
guygg8835 分钟前
结合VD算法与IMM算法的卡尔曼滤波机动目标跟踪方法
人工智能·算法·目标跟踪
R-G-B44 分钟前
BM28 二叉树的最大深度
数据结构·算法·二叉树·bm28·二叉树的最大深度