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.
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
Hint
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.
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
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.
题意:给出N门课的作业截止天数和完成作业需要的天数,每天只能写一门作业,每迟完成一天就要扣一点成绩,输出最少要扣的成绩点数以及安排写作业的顺序。
思路:额,思路不好讲啊,直接看代码注释好了。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct node
{
char s[105]; //课程名字
int d; //截止时间
int c; //完成所需时间
}a[20];
int w[100000][20]; //w[i]表示i的二进制表示 如w[3]=11000000...
int dp[100000]; //dp[i]表示i状态下扣的最少绩点
int day[100000]; //day[i]表示完成i状态时当前天数
int g[100000]; //储存路径
int p[20]; //p[i]=2^i
int ans[20];
void init() //预处理p,w数组
{
memset(w,0,sizeof(w));
p[0]=1;
for(int i=1;i<20;i++)
p[i]=p[i-1]*2;
for(int i=0;i<100000;i++)
{
int temp=i;
for(int j=0;j<20;j++)
{
w[i][j]=temp%2;
temp/=2;
}
}
}
int main()
{
init();
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i].s>>a[i].d>>a[i].c;
}
memset(dp,0x3f3f3f3f,sizeof(dp));
memset(day,0x3f3f3f3f,sizeof(day));
dp[0]=0;
day[0]=0;
g[0]=0;
int sum=pow(2,n); //sum-1表示全部完成时的状态
for(int i=0;i<sum;i++)
{
for(int j=0;j<n;j++)
{
if(w[i][j]==0&&dp[i]!=0x3f3f3f3f) //这表明i状态下第j门作业未完成且已达到i状态
{
int temp=i+pow(2,j); //temp表示在i状态基础上完成j作业后的状态
int x=day[i]+a[j].c-a[j].d; //x表示到达j状态时的天数
if(x<0) x=0;
if(dp[temp]>dp[i]+x) //更新
{
dp[temp]=dp[i]+x;
day[temp]=day[i]+a[j].c;
g[temp]=i; //temp状态是由i状态转移来的,用g数组记录路径
}
}
}
}
cout<<dp[sum-1]<<endl;
int temp=sum-1;
int cnt=n;
while(temp!=0) //由最后一个状态sum-1,利用g数组还原出路径,并记录在ans数组中
{
int res=temp-g[temp]; //若由g[temp]状态到temp状态完成了i作业,则2^i=temp-g[temp]
for(int i=0;i<n;i++) //利用p数组找出i
{
if(res==p[i])
{
ans[--cnt]=i;
break;
}
}
temp=g[temp];
}
for(int i=0;i<n;i++)
{
cout<<a[ans[i]].s<<endl;
}
}
return 0;
}