Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3049 Accepted Submission(s): 1155
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.
Author
Ignatius.L
n<=15,由题意知,只需对这n份作业进行全排列,选出扣分最少的即可。用全排会超时。用一个二进制数存储这n份作业的完成情况,第1.。。。n个作业状况分别对应二进制数的第0,1.。。。。,n-1位则由题意,故数字上限为2^n其中 2^n-1即为n项作业全部完成,0为没有作业完成。。。用dp[i]记录完成作业状态为i时的信息(所需时间,前一个状态,最少损失的分数)。递推条件如下1.状态a能做第i号作业的条件是a中作业i尚未完成,即a&i=0。2.若有两个状态dp[a],dp[b]都能到达dp[i],那么选择能使到达i扣分小的那一条路径,若分数相同,转入33.这两种状态扣的分数相同,那么选择字典序小的,由于作业按字典序输入,故即dp[i].pre = min(a,b);初始化:dp[0].cost = 0;dp[0].pre=-1;dp[0].reduced = 0;最后dp[2^n-1].reduced即为最少扣分,课程安排可递归输出。
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
struct {
string subject;
int deadline;
int cost;
} data[15];
struct {
int time;
int reduce;
int pre;
int num;
} dp[1 << 15];
bool visit[1 << 15];
void output(int cur) {
if (cur == 0 || cur == -1)
return;
output(dp[cur].pre);
cout << data[dp[cur].num].subject << endl;
}
int main(void) {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> data[i].subject >> data[i].deadline >> data[i].cost;
memset(visit, false, sizeof (visit));
dp[0].pre = -1;
dp[0].time = dp[0].reduce = 0;
visit[0] = true;
for (int i = 0; i < (1 << n) - 1; i++) {
for (int j = 0; j < n; j++) {
int cur = 1 << j;
if ((cur & j) == 0) {
int tmp = cur | i;
int time = dp[i].time + data[j].cost;
int reduce = dp[i].reduce;
if (time > data[j].deadline)
reduce += time - data[j].deadline;
if (visit[tmp]) {
if (reduce < dp[tmp].reduce) {
dp[tmp].reduce = reduce;
dp[tmp].pre = i;
dp[tmp].num = j;
dp[tmp].time = time;
}
} else {
visit[tmp] = true;
dp[tmp].reduce = reduce;
dp[tmp].pre = i;
dp[tmp].num = j;
dp[tmp].time = time;
}
}
}
}
cout << dp[(1 << n) - 1].reduce << endl;
output((1 << n) - 1);
}
return 0;
}