Problem Description:
Ignatius have just come back school from the 30th ACM/ICPC. Now he had 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 would reduce his score of the final Test, 1 day for 1 poi Nt. And as you know, doing homework always takes a long time. So Ignatius wants-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 was a single integer T which is the number of test cases. T test cases follow.
Each test case is 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 would at the most have characters) and a integers D (the dead Line of the subject), C (what many days would it take Ignatius to finish this subject ' s homework).
Note:all The subject names is given in the alphabet increasing order. So, may process the problem much easier.
Output:
For each test case, you should output the smallest total reduced score and then give out the order of the subjects, one subje CT in a line. If there is more than one orders, you should output the alphabet smallest one.
Sample Input:
2
3
Computer 3 3
中文版 20 1
Math 3 2
3
Computer 3 3
中文版 6 3
Math 6 3
Sample Output:
2
Computer
Math
中文版
3
Computer
中文版
Math
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <string> #include <queue> #include <stack> #include <set> #include <map># Include <algorithm> #define LL long longusing namespace Std;const int maxn = 16;const int inf = 0x3f3f3f3f;struct Nod e{Char name[20]; int D, C;} Node[maxn];int dp[1<<maxn];int pre[1<<maxn];int n;void output (int status)//in dictionary order (the title input order is the dictionary order) {if ( Status = = 0) return; int t = 0; for (int i=0;i<n;i++) {if (Status & (1<<i))! = 0 && (pre[status] & (1<<i)) = = 0) {T = i; Break }} output (Pre[status]); printf ("%s\n", Node[t].name);} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) {scanf ("%s%d%d", &node[i].name, &node[i]. D, &node[i]. C); } for (int i=0;i< (1<<n); i++) dp[i] = inf; DP[0] = 0;//initialization for (int i=0;i< (1<<n), i++) {for (int j=0;j<n;j++) { if (I & (1<<j)) continue;//If the job is completed then continue int time = 0; for (int k=0;k<n;k++) if (I & (1<<k)) time + = Node[k]. C Time + = Node[j]. c;//statistics until the total time required to complete the job if (Timing > Node[j). D) Time-= Node[j]. D else time = 0; if (dp[i| ( 1<<J)] > Dp[i] + time) {dp[i| ( 1<<J)] = Dp[i] + time; Pre[i| ( 1<<J)] = i; }}} printf ("%d\n", dp[(1<<n)-1]); Output ((1<<n)-1); } return 0;}
HDU 1074 Doing Homework (state compression + DP)