HDUOJ 1074 Doing Homework

题目链接

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

典型的状压 DP ~
我们直接枚举所有情况,每次更新一下最优值即可,用 d p [ i ] . s c o r e dp[i].score dp[i].score 记录最小扣分数,则有状态转移方程:
r e s = m a x ( d p [ k ] . t i m e + c o u r s e [ j ] . c o s t − c o u r s e [ j ] . d e a d l i n e , 0 ) , j ∈ [ 0 , n ) res=max(dp[k].time+course[j].cost-course[j].deadline,0),j\in [0,n) res=max(dp[k].time+course[j].cost−course[j].deadline,0),j∈[0,n)
d p [ i ] . s c o r e = m i n ( d p [ i ] . s c o r e , d p [ k ] . s c o r e + r e s ) dp[i].score=min(dp[i].score,dp[k].score+res) dp[i].score=min(dp[i].score,dp[k].score+res)
但是题目还有一个额外要求,就是输出路径,因为题目已经规定输入样例就是字典序升序的,所以无需考虑字典序。输出可以用 DFS 输出,所以在 DP 的过程中记录每个点(把科目当作结点)的前驱结点和现有结点即可,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
struct node1{char name[105];int deadline,cost;
}course[20];struct node2{int time,score,pre,now;
}dp[1<<15];void print(int k){if(!k) return;print(dp[k].pre);printf("%s\n",course[dp[k].now].name);
}int t,n;
int main(){scanf("%d",&t);while(t--){memset(dp,0,sizeof(dp));scanf("%d",&n);for(int i=0;i<n;i++) scanf("%s%d%d",course[i].name,&course[i].deadline,&course[i].cost);for(int i=1;i<(1<<n);i++){dp[i].score=inf;for(int j=n-1;j>=0;j--){if(i&(1<<j)){int k=i-(1<<j);int res=max(dp[k].time+course[j].cost-course[j].deadline,0);if(dp[k].score+res<dp[i].score){dp[i].score=dp[k].score+res;dp[i].now=j;dp[i].pre=k;dp[i].time=dp[k].time+course[j].cost;}}}}printf("%d\n",dp[(1<<n)-1].score);print((1<<n)-1);}
}

HDUOJ 1074 Doing Homework相关推荐

  1. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

  2. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  3. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  4. [kuangbin]专题12 基础DP

    HDU 1024 Max Sum Plus Plus HDU 1029 Ignatius and the Princess IV HDU 1069 Monkey and Banana HDU 1074 ...

  5. 杭电OJ分类题目(3)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(3) HDU Computational Ge ...

  6. 状态压缩dp学习小记part2

    继续学习状态压缩的相关知识. 本来准备继续按照上篇博文里提到的那篇论文继续学习,但被矩形完全覆盖虐了回来,决定先做些其他的题增进理解之后再回来做. Zoj 3471 Most Powerful 题目链 ...

  7. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  8. 杭电60道DP问题总结(一)

    杭电60道DP问题总结: DP是一个很有艺术的思想.看似简单的背后却隐藏着深刻的含义. 题目连接地址:http://acm.hdu.edu.cn/problemclass.php?id=516& ...

  9. 老鱼的-kuangbin专题题解

    kuangbin专题问题一览 专题一 简单搜索 POJ 1321 棋盘问题 POJ 2251 Dungeon Master POJ 3278 Catch That Cow POJ 3279 Flipt ...

最新文章

  1. CodeForces - 1422E Minlexes(dp+字符串)
  2. 《算法导论》读书笔记--第1、2章课后题 (转)
  3. oracle中文加密算法,Oracle数据库替代加密算法
  4. 一种替代的多生产者方法
  5. Linux下shell命令 1
  6. 什么是面向对象,为什么要面向对象
  7. 对创业者来说,创业心学就是一个不断提升自己
  8. 英伟达CUDA 10终于开放下载了
  9. Java源码分析之CountDownLatch
  10. 绿联串口线linux驱动下载,usb转db9串口线驱动
  11. 不动产登记工作统计难 要靠三维不动产管理系统
  12. c语言编程怎么样,C语言怎么样,好学吗?
  13. abaqus python_abaqus python脚本入门
  14. 妥妥的去面试之Android基础(五)
  15. 新手解决Connections could not be acquired from the underlying database!Java代码上传到服务器连接不上数据库
  16. 餐饮收银软件的O2O转型史
  17. Elsevier 期刊 Editorial Manager (EM)投稿系统踩坑
  18. 沪杭行——君子之交淡如水
  19. 催收系统 源码 php,header.php
  20. 问题:在什么场合应当用几何平均值,而不是用算术平均值?!

热门文章

  1. Matrix的数学原理
  2. vacuum清理数据库
  3. 【pmp】事业环境因素和组织过程资产区别
  4. Flink 笔记二 Flink的State--状态原理及原理剖析
  5. 第6套:大一-物流跟踪管理系统
  6. 关于html5 video标签手机无法播放问题
  7. Golang Excel表格的解析、导出
  8. webstorm tab键补全代码改为enter
  9. js中this指向问题总结
  10. html5获取播放器实时进度