Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14670 Accepted Submission(s): 7100
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.
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
题意:求以某种顺序完成作业时,扣分最少,且作业名称的字典序最小。
思路:由于 (1<=N<=15) 的范围很小,状压DP很合适,所以考虑用dp[ i ] (0 <= i < ( 1 << N ) ) 代表每个状态的扣分的最小值,对于每个状态 i , 我们可以枚举其每个已完成的作业 j ,然后求出以每个作业 j 为最后一门完成时的最小值即可。 对于输出完成作业的顺序,具体请看代码。
//#include <bits/stdc++.h>
#include <map>
//#include <tr1/unordered_map>
#include<limits>
#include <float.h>
#include <list>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
inline int lowbit(int x){ return x & (-x); }
inline int read(){int X = 0, w = 0; char ch = 0;while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();return w ? -X : X;}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x)
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x)
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define db double
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int maxp = 1e3 + 10;
const LL INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 20090717;
const int MAXN = (1<<15) + 10;
int n;
char cname[20][150]; //课程名称
int deadline[20], cost[20], dp[MAXN], finish[MAXN], pre[MAXN];
//对应的分别是:课程截止时间、课程时间花费、各状态扣分最小值、各状态完成时间、各状态最优时以何门作业最后完成
void output(int x){
if (!x) return;
output(x-(1<<(pre[x]-1)));
printf("%s\n", cname[pre[x]]);
}
int main()
{
int t = read();
while (t--){
n = read();
int ma = (1<<n)-1;
For (i, 1, n) scanf("%s%d%d", cname[i], &deadline[i], &cost[i]);
For (i, 1, ma){
dp[i] = inf; //初始化到达转态 i 的扣分
_For (j, n, 1){ // j ( n -> 1 ) 逆序枚举,是为了让字典序较大的作业最后完成,就满足了字典序较小的要求,
//且后面的判断为 dp[i] > dp[i-temp] + add ,保证了即使有个 j' (j' < j),拥有和以
//第 j 门作业最后完成时的最小值,也不会是最优解,保证了字典序较大的作业最后完成。
int temp = (1<<(j-1)); //第 j 门在转态 i 的对应位置
if (!(i&temp)) continue; //若转态 i 中没有完成作业 j,则跳过
int add = finish[i-temp] + cost[j] - deadline[j]; //以第 j 门作业最后完成时的将要扣的分
if (add < 0) add = 0; //在截止时间内完成,不需要扣分
if (dp[i] > dp[i-temp] + add){
dp[i] = dp[i-temp] + add;
finish[i] = finish[i-temp] + cost[j];
pre[i] = j; //记录转态 i 扣分最少时,是以何门作业最后完成
}
}
}
printf("%d\n", dp[ma]);
output(ma);
}
return 0;
}