State compression ----- hdu1074 doing homework

Source: Internet
Author: User
Hdu1074 doing homework

  

Question: I gave n homework assignments, and then gave each homework a completion time and spend. If the completion time exceeds the time limit, I would deduct the score, then let you find an optimal solution to minimize the number of points deducted. When there are multiple solutions, the one with the smallest Lexicographic Order is output, the meaning of the question has already been said that the names of homework are input in Lexicographic Order from small to large, so it is much better to deal with it.

Analysis: the Key to this question is how to record the path. For details, refer to the code.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5  6 using namespace std; 7 #define MAX 1<<15+1 8 int N; 9 struct Node{10 char name[108];11 int deadline;12 int cost;13 }homework[16];14 struct START15 {16     int time;17     int pre;18     int score;19     int now;20 } dp[MAX];21 int record[16];22 23 void DP()24 {25     int Final=(1<<N)-1,punish,previous;26     dp[0].id=dp[0].now=dp[0].pre=dp[0].score=dp[0].time=0;27     for(int i=1;i<=Final;i++)28     {29         dp[i].score=10000000;30         for(int j=0;j<N;j++)31             if(i&(1<<j))32         {33              previous=(i-(1<<j));34             if(dp[previous].time+homework[j].cost>homework[j].deadline)35                 punish=dp[previous].time+homework[j].cost-homework[j].deadline;36             else punish=0;37             if(dp[i].score>=dp[previous].score+punish)38             {39                 dp[i].score=dp[previous].score+punish;40                 dp[i].time =dp[previous].time+homework[j].cost;41                 dp[i].pre=previous;42                 dp[i].now=j;43             }44         }45     }46     printf("%d\n",dp[Final].score);47     int cal=Final,num=0;48     while(cal)49     {50         record[num++]=dp[cal].now;51         cal=dp[cal].pre;52     }53     for(int i=num-1;i>=0;i--)54     puts(homework[record[i]].name);55 56 }57 int main()58 {59    int T;60    scanf("%d",&T);61    while(T--)62    {63        scanf("%d",&N);64        for(int i=0;i<N;i++)scanf("%s%d%d",homework[i].name,&homework[i].deadline,&homework[i].cost);65        DP();66    }67     return 0;68 }
View code

It was frustrating to do this. In DP (), J was written as J + 1, which was not visible for a long time. After debugging for nearly one hour, I found it.

1 void dp () 2 {3 int final = (1 <n)-1, punish, previous; 4 DP [0]. id = DP [0]. now = DP [0]. pre = DP [0]. score = DP [0]. time = 0; 5 for (INT I = 1; I <= final; I ++) // The status ranges from 0 to 6 {7 DP [I]. score = 10000000; // first initialized to a relatively large value of 8 for (Int J = 0; j <n; j ++) to find the minimum value) // question No. 9 if (I & (1 <j) // question J in status I, why is this written because every possibility is enumerated at 10 // when the status is full, that is, the status is converted from previous to J11 {12 previous = (I-(1 <j )); // only one job is different from previous to I. // if the total time from previous to another question exceeds the deadline of job J, record points 14 // otherwise 0 15 if (DP [previous]. time + homework [J]. cost> homework [J]. deadline) 16 punish = DP [previous]. time + homework [J]. cost-Homework [J]. deadline; 17 else punish = 0; 18 // the minimum value of 19 if (DP [I] When the status is I. score> = DP [previous]. score + punish) 20 {21 DP [I]. score = DP [previous]. score + punish; 22 DP [I]. time = DP [previous]. time + homework [J]. cost; 23 DP [I]. pre = previous; 24 DP [I]. now = J; 25} 26} 27} 28 printf ("% d \ n", DP [Final]. score); 29 // The display path now indicates that status I is formed by a question numbered now, and previous is the previous state of I, 30 int Cal = final, num = 0; 31 While (CAL) 32 {33 record [num ++] = DP [Cal]. now; 34 Cal = DP [Cal]. pre; 35} 36 for (INT I = num-1; I> = 0; I --) 37 puts (homework [Record [I]. name); 38 39}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.