http://acm.hdu.edu.cn/showproblem.php?pid=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
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.

 
题意:有N门课程的作业需要完成,每科作业都有一个deadline和一个消耗的时间,如果完成该门课程的作业的时间超过deadline,则每超过一天就会扣一分,求如何安排完成作业的次序才能使得完成所有作业扣的分数最少,最少分数是多少,并输出做作业的次序。
思路:第一道状压DP。一开始傻傻地想用贪心去做,后来发现不可行,想了挺久才看题解,原来是忌惮了很久的状压DP,因为有挺多的位运算搞不太懂,然后自己恶补了一下这方面知识,算作模模糊糊地搞懂了吧。因为N很小,可以枚举所有情况。5可以二进制表示为1001,从右往左数,该状态表示完成了第一门和第四门作业,就是用1或者0来表示该点有没有经过,然后枚举1<<i(i<N)就相当于枚举每一科的作业, now&i == 0 表示第i+1门作业没完成,now是已经完成了的作业的集合。就这样可以进行DP求解了。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <stack>
#include <iostream>
using namespace std;
#define N 15
#define INF 100000000
struct node
{
int dead,cost;
char s[];
}course[N+];
struct P
{
int pre,score,now,time;
//pre记录完成某一个学科的作业之前已经完成的学科的作业的集合
//now记录当前更新的学科,pre和now用作路径输出
//score是减少的学分的总数,time是当前时间
}dp[<<N];
int vis[<<N]; int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
cin>>course[i].s>>course[i].dead>>course[i].cost;
}
int en=(<<n)-;
for(int i=;i<en;i++){
for(int j=;j<n;j++){
int temp=<<j;
if((i&temp)==){//如果当前的学科作业还没完成
int cur=i|temp;//当完成该科作业时的情况
dp[cur].time=dp[i].time+course[j].cost;//当前使用的时间
int b=dp[i].time+course[j].cost-course[j].dead;
if(b<) b=;
if(vis[cur]){//如果已经完成过该作业就更新
if(dp[cur].score>dp[i].score+b){
dp[cur].score=dp[i].score+b;
dp[cur].pre=i;//还没完成该学科之前已经完成了的学科
dp[cur].now=j;//当前的学科
}
}
else{//如果没完成直接更新
vis[cur]=;
dp[cur].score=dp[i].score+b;
dp[cur].pre=i;
dp[cur].now=j;
}
}
}
}
cout<<dp[en].score<<endl;
stack <int> S;
//用栈输出完成作业的路径,也可以用递归
while(en>){
S.push(dp[en].now);
en=dp[en].pre;
}
while(!S.empty()){
cout<<course[S.top()].s<<endl;
S.pop();
}
}
return ;
}

2016-06-26

 

HDU 1074:Doing Homework(状压DP)的更多相关文章

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

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

  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

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

  4. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu_1074_Doing Homework(状压DP)

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

  7. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  8. HDU 1074 Doing Homework(像缩进DP)

    Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...

  9. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  10. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

随机推荐

  1. Android 5.0(L) ToolBar(替代ActionBar) 现实(四)

    经过三天休息,我回来了,我们继续讨论Toolbar. 在此之前假设您正在步步紧跟下来的序列,然后,你应该注意到MainActivity据说他已被警告.因为他们,我们声明toolbar对象.但一直没有用 ...

  2. ARM中 __IO的作用解析

    __IO在头文件中预定义 #define __IO volatile volatile 影响编译器编译的结果, 指出:volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不要 ...

  3. vs2017 cordova调试android app

    方案是:virtualbox + androidx86 7.0+_x64.iso虚拟机方式

  4. 改善C#程序的建议6:在线程同步中使用信号量

    原文:改善C#程序的建议6:在线程同步中使用信号量 所谓线程同步,就是多个线程之间在某个对象上执行等待(也可理解为锁定该对象),直到该对象被解除锁定.C#中对象的类型分为引用类型和值类型.CLR在这两 ...

  5. winform子容器随父容器的变化设置

    在设计winform窗体时,因为会很少去调整窗体的大小,这时子控件就会出很尴尬的情况, 通过查看空间的属性,发现有这样两个属性,dock和anchor.这里主要说anchor,官方 解释没太看懂,我的 ...

  6. Channel 9视频整理【1】

    David Dong 微软mvp https://www.​facebook.com/​DotNet​Walker http://s​tudyhost.​blogspot.tw/ https://ch ...

  7. ASP.NET MVC控制器Controller

    控制器的定义 MVC模式下的控制器(Controller)主要负责响应用户的输入,并且在响应时可能的修改模型(Model). 之前的URL访问,通常是通过指定服务器的路径来实现,如访问URL:http ...

  8. MongoDB对文档的操作

    插入文档 db.COLLECTION_NAME.insert({doc1},{doc2},...) e.g.:db.collection.insert({name:'123',age:12},{nam ...

  9. WIFI Manager

    Vistumbler - wifi managerhttps://www.vistumbler.net/downloads.htmlhttps://github.com/RIEI

  10. TCP打洞和UDP打洞的区别 (相互直接访问)

    为什么网上讲到的P2P打洞基本上都是基于UDP协议的打洞?难道TCP不可能打洞?还是TCP打洞难于实现?     假设现在有内网客户端A和内网客户端B,有公网服务端S.     如果A和B想要进行UD ...