题目链接:http://acm.split.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): 10195    Accepted Submission(s): 4900

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.

 
Author
Ignatius.L
 
 
 
 
代码如下:
  #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = +; struct node
{
string name;
int deadline, cost;
}subject[MAXN];
int dp[<<], id[<<], pre[<<];
//id[status]表示当前的状态是由添加哪一科而得来的, id[status]的值即subject数组的下标。
//pre[status]表示当前的状态是由哪一个状态转移过来的。 void Print(int s)
{
if(!s) return;
Print(pre[s]); //追溯上一个状态
cout<< subject[id[s]].name <<endl;
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<n; i++)
cin>>subject[i].name>>subject[i].deadline>>subject[i].cost; memset(dp, -, sizeof(dp));
dp[] = ;
for(int s = ; s<(<<n); s++)
{
int time = ;
for(int i = ; i<n; i++) //计算当前时间
if(s&(<<i))
time += subject[i].cost; for(int i = ; i<n; i++)
{
if(!(s&(<<i)))
{
int exceeded = time + subject[i].cost - subject[i].deadline; //计算超出时间
if(exceeded<) exceeded = ;
int tmp = s|(<<i);
if(dp[tmp]==- || dp[tmp]>dp[s]+exceeded) //更新
{
dp[tmp] = dp[s] + exceeded;
id[tmp] = i;
pre[tmp] = s;
}
}
}
} printf("%d\n", dp[(<<n)-]);
Print((<<n)-);
}
}

HDU1074 Doing Homework —— 状压DP的更多相关文章

  1. hdu_1074_Doing Homework(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...

  2. HDU 1074 Doing Homework 状压dp(第一道入门题)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. HDU 1074 Doing Homework (状压DP)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. HDU 1074 Doing Homework 状压DP

    由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...

  5. kuangbin专题十二 HDU1074 Doing Homework (状压dp)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. HDU1074:Doing Homework(状压DP)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

  8. Doing Homework HDU - 1074 (状压dp)

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

  9. HDU 1074:Doing Homework(状压DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Problem Description Ignatius has just ...

随机推荐

  1. Shell脚本问题详解

    例1:找出当前系统中端口大于1024的程序! 使用netstat -tuln查询出的结果如下,需要输出红色字体的行: [root@localhost ~]# netstat -tuln Active ...

  2. hexo干货系列:(五)hexo添加站内搜索

    前言 本来想用百度站内搜索,但是没成功,所以改用swiftype,用起来还是很棒的,这里分享一下我的安装步骤 正文 注册 去swiftype官网注册个账号,然后登陆,对了不要去在意30天试用,30天过 ...

  3. bzoj 2802 [Poi2012]Warehouse Store STL

    [Poi2012]Warehouse Store Time Limit: 10 Sec  Memory Limit: 64 MBSec  Special JudgeSubmit: 621  Solve ...

  4. 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon

    floyd-warshall算法 通过dp思想 求任意两点之间最短距离 重复利用数组实现方式dist[i][j] i - j的最短距离 for(int k = 1; k <= N; k++) f ...

  5. [NOIP1999] 提高组 洛谷P1014 Cantor表

    题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...

  6. 【BZOJ2006】超级钢琴(RMQ,priority_queue)

    题意: 思路: 用三元组(i, l, r)表示右端点为i,左端点在[l, r]之间和最大的区间([l, r]保证是对于i可行右端点区间的一个子区间),我们用堆维护一些这样的三元组. 堆中初始的元素为每 ...

  7. 50个必备的实用jQuery代码段(转载)

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...

  8. Eclipse同时显示多个控制台项目的输出

    操作步骤: 1.运行项目1,运行项目2 2.在Exlipse中选择这两个的控制台进行切换

  9. Python开发的一个IDE推荐,Sublime Text 3

    Sublime Text 3 官网下载地址为, LINK. 目前最新版本是3114. 这里转载泱泱长空的授权文件(注册码)文章[1],将几个可以用的注册码列举如下: 补充:2016.05 最近经过测试 ...

  10. spring配置文件加密

    原文:http://www.open-open.com/code/view/1453520072183 spring框架在一些对安全性要求较高的生产环境下,配置文件不允许出现明文用户名密码配置,如数据 ...