标签:help 题解 cstring cost red wan dea 需要 cat
题目链接:https://vjudge.net/problem/HDU-1074
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。第一次写这种题目人都傻了,心里就***。看别人题解还看不懂,随后去了解学习了一下状压dp https://www.cnblogs.com/Tony-Double-Sky/p/9283254.html
这题就是用01来代表科目的完成,0101代表第2、4科目是完成的1、3未完成,那么所有的完成情况我们可以用二进制来进行表示 一共为1<<n种
拥有了所有的情况就可以dp了
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 using namespace std; 6 const int maxn = 20; 7 int dp[1<<maxn],fa[1<<maxn],cost[1<<maxn],d[maxn],c[maxn]; 8 string s[maxn]; 9 void print(int x){ 10 if(!x) return ; 11 print(x-(1<<fa[x])); 12 cout<<s[fa[x]]<<endl; 13 } 14 int max(int a,int b)//不定义的话vj无法识别 15 { 16 if(a>b) return a; 17 else return b; 18 } 19 int main() 20 { 21 int t,n; 22 cin>>t; 23 while(t--){ 24 cin>>n; 25 for(int i=0;i<n;i++) cin>>s[i]>>d[i]>>c[i]; 26 for(int i=1;i<(1<<n);i++){//遍历所有的完成情况 27 dp[i]=0x3f3f3f3f; 28 for(int j=n-1;j>=0;j--){ 29 int mid=(1<<j); //选择一门课程 30 if(!(i&mid)) continue;//筛选所有方法中这门课程完成的情况 31 int val=0;//超出的时间最初设为0 32 if(val<cost[i-mid]+c[j]-d[j])//需要的总时间减去这门作业完成多出来的时间 33 val=max(0,cost[i-mid]+c[j]-d[j]);//超时对于0则更新 34 if(dp[i]>dp[i-mid]+val){//dp代表着这种情况超出的时间 35 dp[i]=dp[i-mid]+val; 36 cost[i]=cost[i-mid]+c[j];//所需要的总时间 37 fa[i]=j;//最小时间对应的限一个状态 38 } 39 } 40 } 41 cout<<dp[(1<<n)-1]<<endl; 42 print((1<<n)-1); 43 } 44 }
下次我还要来重新看看做做这题
[kuangbin带你飞]专题1-23 Doing Homework HDU - 1074
标签:help 题解 cstring cost red wan dea 需要 cat
原文地址:https://www.cnblogs.com/waaaaaa/p/13048903.html