hdu 【1074】Doing Homework
2016-03-29 19:57
232 查看
Doing Homework
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7256 Accepted Submission(s): 3202[/b][align=left]Problem Description[/align]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 teacherwill 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.[align=left]Input[/align]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.[align=left]Output[/align]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.[align=left]Sample Input[/align]23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3[align=left]Sample Output[/align]
2ComputerMathEnglish3ComputerEnglishMathHintIn the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but theword "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.
/* 首先大家要理解什么是位运算,1<<n=2^n;相当于在1的右边加n个0,这道题我还是利用递推,但是状态不是很好想,要利用2进制对状态进行表示。n个作业全部做完,相当于2进制表示的n个1,对于每个作业是是否已经做了,我们只需要利用位运算i&&(1<<j)是否为0即可求出来。这里共有1<<n个状态;*/#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <stack>using namespace std;struct course{char name[105];int dead,cost;}a[20];struct Node{int cost;int pre;int reduced;}dp[1<<16];bool vis[1<<16]; //判断是否这个状态是否走过int main(){int T,n;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i = 0; i < n; i++)scanf("%s%d%d",a[i].name, &a[i].dead, &a[i].cost);memset(vis,false,sizeof(vis));dp[0].cost = 0;dp[0].pre = -1;dp[0].reduced = 0;vis[0] = true;int upper = 1 << n;for(int i = 0; i < upper; i++){for(int j = 0; j < n; j++){int bin = 1<<j;if((i&bin) == 0){int temp = i|bin;int cost = dp[i].cost + a[j].cost ;int reduce = cost - a[j].dead;if(reduce < 0) reduce = 0; //有可能有负数reduce += dp[i].reduced;if(vis[temp] && reduce < dp[temp].reduced){dp[temp].cost = cost;dp[temp].pre = j;dp[temp].reduced = reduce;}else if(!vis[temp]){vis[temp] = true;dp[temp].cost = cost;dp[temp].pre = j;dp[temp].reduced = reduce;}}}}printf("%d\n",dp[--upper].reduced);stack <int> S;int temp = dp[upper].pre;while(temp != -1){S.push(temp);upper -= 1 << dp[upper].pre; //这里我直接减去,相当于去掉特定位置的1temp = dp[upper].pre;}while(!S.empty()){printf("%s\n",a[S.top()].name);S.pop();}}return 0;}
相关文章推荐
- java你好! 我的java学习之路 Day2
- Communication Patterns
- 构造方法和析构方法
- 第五周上机实践项目 项目1--三角形类雏形(2)
- JackJsonUtils工具类
- 二值网络(Binarized Neural Networks)学习与理解
- HDU——1846Brave Game(巴什博弈)
- 递归问题之分割问题
- 1.3算法入门之打渔晒网
- 利用Oracle分析函数实现多行数据合并为一行
- 2018
- linux 递归生成两个目录的diff
- 成绩处理
- Android 天气预报
- 项目16-时间类(2)
- 深入理解php内核
- 20160329中艺收盘总结
- C#使用黑体字
- Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输
- 2Strange fuction