HDU1074——Doing Homework(状态压缩dp)
来源:互联网 发布:企业数据库系统有哪些 编辑:程序博客网 时间:2024/06/06 05:00
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9323 Accepted Submission(s): 4405
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.
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
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
Sample Output
2ComputerMathEnglish3ComputerEnglishMathHintIn 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
Recommend
简单状态压缩dp#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>#include <vector>#include <map>#include <set>using namespace std;#define _ ios::sync_with_stdio(false)typedef long long ll;const int MAXN=20;const int INF=1e9+7;struct hom{ string name; int d,t;}a[MAXN];int cmp(hom x,hom y){ return x.name>y.name;}int dp[1<<20];int sum[1<<20];int pre[1<<20];void dfs(int s){ if(s==0) return; int x=pre[s]; int temp=s&(~(1<<x)); dfs(temp); cout<<a[pre[s]].name<<endl;}int main(){ _; int T; cin>>T; while(T--){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i].name>>a[i].d>>a[i].t; } sort(a,a+n,cmp); memset(sum,0,sizeof(sum)); for(int i=0;i<1<<n;i++){ dp[i]=INF; for(int j=0;j<n;j++){ int temp=1<<j; if(i&temp){ sum[i]+=a[j].t; } } } dp[0]=0; for(int i=0;i<1<<n;i++){ for(int j=0;j<n;j++){ int temp=(1<<j); if(temp&i){ int x=i&(~temp); if(sum[x]+a[j].t>a[j].d){ if(dp[x]+sum[x]+a[j].t-a[j].d<dp[i]){ pre[i]=j; dp[i]=dp[x]+sum[x]+a[j].t-a[j].d; } }else{ if(dp[i]>dp[x]){ pre[i]=j; dp[i]=dp[x]; } } } } } int temp=((1<<n)-1); cout<<dp[temp]<<endl; dfs(temp); }}
阅读全文
0 0
- HDU1074——Doing Homework(状态压缩DP)
- HDU1074——Doing Homework(状态压缩dp)
- hdu1074 Doing Homework(状态压缩dp)
- hdu1074 Doing Homework (状态压缩dp)
- HDU1074 Doing Homework 状态压缩DP
- HDU1074:Doing Homework(状态压缩DP)
- HDU1074 Doing Homework 状态压缩DP
- 状态压缩dp-HDU1074-Doing Homework
- HDU1074 Doing Homework(状态压缩DP)
- HDU1074:Doing Homework(状态压缩DP)
- HDU1074 Doing Homework 状态压缩dp
- HDU1074:Doing Homework(状态压缩DP)
- HDU1074:Doing Homework-状态压缩DP
- hdu1074 Doing Homework(状态压缩DP Y=Y)
- D - Doing Homework HDU1074 ( 动态规划 + 状态压缩 )
- HDU1074 Doing Homework(状压DP~状态的二进制表示)
- 【HDU1074 dp状态压缩】Doing Homework 第一道状态压缩dp
- Doing Homework HDU1074 【dp】
- 安装配置MySQL5.7.17版本
- Unreal 4 Layered material 分层材质
- Java程序内存分析:使用mat工具分析内存占用
- 《最长的一帧》理解01_场景渲染
- 集合与数组浅析
- HDU1074——Doing Homework(状态压缩dp)
- 清除浮动
- uva1396 Most Distant Point from the Sea
- webpack中与样式相关的loader
- 深入浅出学Hive
- Spring MVC 知识点集合
- mysql优化建议100条
- acm编程训练 单向链表 C语言程序设计教程(第三版)课后习题11.8
- POJ1979 Red and Black(DFS)