D-doing Homework
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit Status Practice HDU 1074
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
Hint
In the second Test case, both Computer->english->math and computer->math->english leads to reduce 3 points, BU t the word "中文版" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.
The meaning of the title to You N (n<=15) course, you have to complete each course of homework, but the teacher gave you the completion of the deadline, more than the deadline every day will deduct you a point. Ask you to schedule the completion of the job in order to minimize the deduction.
The first time to do state compression, reference analysis: Click to open the link
Dp[i]: Indicates the smallest fraction that reaches the state I deducted, Sum[i]: Indicates the number of days past when the status I arrived
#include <bits/stdc++.h>using namespace Std;template<class t>inline T Read (t&x) {char C; while ((C=getchar ()) <=32) if (c==eof) return 0; BOOL Ok=false; if (c== '-') Ok=true,c=getchar (); for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 '; if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);} Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x; if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');} Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1<<20;CONST double inf=999999999; #define Lson (rt<<1), l,m# Define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long Ll;typedef Double Db;typedef pair<int,int> P; #define BUG printf ("---\ n"); #define MOD 100000000struct node{char name[150]; int need;//takes a few days to solve the term}p[maxn];int dp[maxn],pre[maxn],sum[maxn];void to (int s) {if (!s) return of int length;//teacher) To (s^ (1<<pre[s)); printf ("%s\n", P[pre[s]].name);} int main () {int t,n; Read (T); while (t--) {read (n); for (i,0,n) {scanf ("%s%d%d", p[i].name,&p[i].length,&p[i].need); } memset (Sum,0,sizeof (sum)); int n=1<<n; for (int i=1;i<n;i++) {dp[i]=inf; for (int j=0;j<n;j++) {int tmp=1<<j;if (!) ( Tmp&i)) continue; int reduce=sum[i^tmp]+p[j].need-p[j].length; if (reduce<=0) reduce=0; if (Dp[i]>=reduce+dp[i^tmp]) {dp[i]=reduce+dp[i^tmp]; Pre[i]=j; SUM[I]=P[J].NEED+SUM[I^TMP]; } }} writeln (Dp[n-1]); to (N-1); } return 0;}
D-doing Homework HDU1074 (Dynamic programming + state compression)