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.
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.
#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