链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074

Doing Homework

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

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.

 
最开始想用贪心做,没能做出。看了题解说是状态压缩dp,看完题解自己写了一遍。
思路很好理解,用15位的二进制数来表示所有状态。
中间有一个存路径的操作,以后可以借鉴。
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<stack>
  6. using namespace std;
  7.  
  8. const int INF=<<;
  9.  
  10. struct Node
  11. {
  12. char name[];
  13. int dead,cost;
  14. }cla[];
  15.  
  16. struct DP
  17. {
  18. int pre,time,spent,now;
  19. }dp[<<];
  20.  
  21. int main()
  22. {
  23. int t,i,j,s,n,end;
  24. //scanf("%d",&t);
  25. cin>>t;
  26. while(t--)
  27. {
  28. memset(dp,,sizeof(dp));
  29. //scanf("%d",&n);
  30. cin>>n;
  31. for(int i=;i<n;i++)
  32. //scanf("%s%d%d",cla[i].name,&cla[i].dead,&cla[i].cost);
  33. cin>>cla[i].name>>cla[i].dead>>cla[i].cost;
  34. int en=<<n;
  35. for(int s=;s<en;s++)
  36. {
  37. dp[s].spent=INF;
  38. for(int i=n-;i>=;i--)
  39. {
  40. int tem=<<i;
  41. if(s&tem)
  42. {
  43. int past=s-tem;
  44. int st=dp[past].time+cla[i].cost-cla[i].dead;
  45. if(st<)
  46. st=;
  47. if(dp[s].spent>dp[past].spent+st)
  48. {dp[s].spent=dp[past].spent+st;
  49. dp[s].now=i;
  50. dp[s].pre=past;
  51. dp[s].time=dp[past].time+cla[i].cost;
  52.  
  53. }
  54. }
  55. }
  56. }
  57. stack<int> S;
  58. int tem = en-;
  59. cout << dp[tem].spent << endl;
  60. while(tem)
  61. {
  62. S.push(dp[tem].now);
  63. tem = dp[tem].pre;
  64. }
  65. while(!S.empty())
  66. {
  67. cout << cla[S.top()].name << endl;
  68. S.pop();
  69. }
  70. }
  71. return ;
  72. }

HDU_1074_Doing Homework_状态压缩dp的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  8. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. yarn-cli 缓存

    yarn cache list Yarn 会在你的用户目录下开辟一块全局缓存用以保存下载的包.yarn cache list 用于列出所有已经缓存的包. yarn cache dir 执行 yarn ...

  2. distcp导致个别datanode节点数据存储严重不均衡分析

    hadoop2.4生产集群已经执行一段时间了.因为大量的hadoop1.0上面的应用不断迁移过来.刚開始事hdfs这边还没有出现多少问题.随着时间的推移,近期发现个别的datanode节点上面的磁盘空 ...

  3. shell脚本 加密备份MySQL数据库

    1.加密备份为.bak文件(实际只是个.zip文件) #!/bin/bash # $:IP地址 # $:用户名 # $:数据库密码 # $:数据库名 # $:加密密码 # $:备份文件名 mysqld ...

  4. Ext sqlserver C# 数据库备份还原代码,给大家参考下

      <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %& ...

  5. Centos下mahout安装与配置

    对于Mahout的安装与配置,须要一个前提.就是hadoop已经安装. 假设没有安装能够參考. http://blog.csdn.net/u012965373/article/details/4533 ...

  6. _DataStructure_C_Impl:图的遍历

    #include<stdio.h> #include<stdlib.h> #include<string.h> //图的邻接表类型定义 typedef char V ...

  7. Android源代码文件夹结构说明

    在学习Android的过程中,学习写应用还好.一開始不用管太多代码.直接调用函数就能够了,可是工作中却须要改动到framework之类的东东 所以感觉開始纠结了,又是初学,非常多不懂,所以就去找了关于 ...

  8. mybatis之if else语句

    最近项目中遇到一个相同表设计,但是表名不同的sql语句操作. 在遇到这样的情况时候可以用一下方式: <choose> <when test=""> //.. ...

  9. HTTP要点概述:七,编码,压缩传输,分块传输

    一,编码: HTTP 在传输数据时可以按照数据原貌直接传输,但也可以在传输过程中通过编码提升传输速率.通过在传输时编码,能有效地处理大量的访问请求.但是,编码的操作需要计算机来完成,因此会消耗更多的 ...

  10. HIbernate- SQLQuery 简易

    package cn.demo; import java.util.Arrays; import java.util.List; import org.hibernate.SQLQuery; impo ...