1. 程式人生 > >HDU 1074 Doing Homework 狀態壓縮DP

HDU 1074 Doing Homework 狀態壓縮DP

output -- max 方式 sch adl struct work small

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1074

題目大意:小明打完比賽回來補作業,N個作業每個作業有 名字,截止日期,完成所需時間 三個屬性,如果完成時間>截止日期,就會被扣分,多一天扣一分。現在你幫他安排做作業計劃。N < 15

解題思路:其實很明確的是這是個決策性問題。每次都要做一個決策,從N個作業中選出來一個來做,那麽問題就是多個決策,每個決策有N種可選,如何做出決策? 那麽就是典型的DP了。

狀態:已經完成了m個作業------->>接下來要做第m+1個作業

轉移:可以從已經做得作業中選出來m個,看已用時間,然後計算做第m+1個作業的扣分情況。

那麽dp[S] :=完成S集合中的作業所扣的分數最小值。

dp[S] = min(dp[S^X] + S^X用時-第X個作業截止日期)  X 為1 ~ N中的任意一個作業且 S & X != 0

由於N < 15,所以可以采用狀態壓縮方式,每一位表示一個作業,1為選擇了,0為未選擇。

代碼:

 1 const int maxn = 20;
 2 const int maxm = (1 << 15) + 5;
 3 struct sub{
 4     int dl, ti, name;
 5 };
 6 char str[maxn][105];
 7 sub subs[maxn];
8 int n; 9 int csb[maxn], dp[maxm], tis[maxm], pre[maxm], curs[maxm]; 10 11 void solve(){ 12 for(int i = 1; i <= n; i++) csb[i] = 1 << (i - 1); 13 14 memset(dp, 0x3f, sizeof(dp)); 15 memset(tis, 0, sizeof(tis)); 16 memset(pre, 0, sizeof(pre)); 17 18 const int cnt = 1
<< n; 19 for(int i = 1; i < cnt; i++){ 20 int x = i, st = 1; 21 while(x){ 22 tis[i] += (x & 1) * subs[st].ti; 23 x >>= 1; st++; 24 } 25 } 26 dp[0] = 0; 27 for(int i = 1; i < cnt; i++){ 28 for(int j = 1; j <= n; j++){ 29 int tmp = tis[csb[j] ^ i] + subs[j].ti - subs[j].dl; 30 if(tmp < 0) tmp = 0; 31 if((i & csb[j]) && dp[csb[j] ^ i] + tmp <= dp[i]){ 32 dp[i] = dp[csb[j] ^ i] + tmp; 33 pre[i] = csb[j] ^ i; 34 curs[i] = j; 35 } 36 } 37 } 38 printf("%d\n", dp[cnt - 1]); 39 stack<int> s; 40 int st = cnt - 1; 41 int sst = curs[st]; 42 while(st > 0){ 43 s.push(subs[sst].name); 44 st = pre[st]; 45 sst = curs[st]; 46 } 47 while(!s.empty()){ 48 int u = s.top(); s.pop(); 49 printf("%s\n", str[u]); 50 } 51 52 return; 53 } 54 int main(){ 55 int T; 56 scanf("%d", &T); 57 while(T--){ 58 scanf("%d", &n); 59 for(int i = 1; i <= n; i++){ 60 scanf("%s %d %d", str[i], &subs[i].dl, &subs[i].ti); 61 subs[i].name = i; 62 } 63 solve(); 64 } 65 }

題目:

Doing Homework

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9876 Accepted Submission(s): 4725


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 Hint 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.

Author Ignatius.L

HDU 1074 Doing Homework 狀態壓縮DP