Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8320 Accepted Submission(s): 3842
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
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 MathHintIn 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.
思路:15门课,可以考虑状态压缩来枚举,en=1<<15 将每种课程的组合方式枚举一下,相与等于1表示当前课程没有完成,可以考虑是否在当前组合下完成它
需要注意一下输出技巧,题目中很好的一点是 Hint 已经说了给的数据都按字典序排列好了
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#define INF 1<<25
using namespace std;
struct Node
{
string na;
int ded,last;
}f[200];
struct knode
{
int now,pre,cost,time; //指向当前,指向上一个状态,花费,时间
}dp[1<<15+1];
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>f[i].na>>f[i].ded>>f[i].last;
}
memset(dp,0,sizeof dp);
int en =1<<n;
for(int s=1;s<en;s++)
{
dp[s].cost=INF;
for(int i=n-1;i>=0;i--) //逆向枚举是为了方便后面按字典序输出
{
int tmp=1<<i;
if(s&tmp)
{
int last=s-tmp;
int ff=dp[last].time+f[i].last-f[i].ded;
if(ff<0)
ff=0;
if(dp[last].cost+ff<dp[s].cost)
{
dp[s].cost=dp[last].cost+ff;
dp[s].now=i;
dp[s].pre=last;
dp[s].time=dp[last].time+f[i].last;
}
}
}
}
stack<int>st;
int tmp=en-1;
cout<<dp[tmp].cost<<endl;
while(tmp) //从后指向前,逆向入栈,后面出栈就能变成字典序
{
st.push(dp[tmp].now);
tmp=dp[tmp].pre;
}
while(!st.empty())
{
cout<<f[st.top()].na<<endl;
st.pop();
}
}
return 0;
}