HDU 1074 Doing Homework (DP State compression)

Source: Internet
Author: User

Test instructions have n jobs to give you the deadline for each assignment and the time it takes for the job to be completed each day after the deadline will be deducted one point can only be finished one job and then do another homework ask to finish all the homework at least how many points deducted

jobs up to only 15 see this number is easy to think of is the state compression Dp[i] indicates that I corresponds to the state of the minimum deduction I converted to a bit of binary after 1 indicates that the bit corresponding job has been done for 0 bit not done so Dp[i] = min{dp[k] + cost | K is the state that turns a certain one into 1 and equals I } 

Since you want to print the path all also need to record the previous state of each state pre

#include <cstdio> #include <cstring>using namespace Std;const int N = +, M = 1 << n;int d[n], c[n], n;in    T dp[m], pre[m], T[m];char s[n][105];void print (int k) {if (k = = 0) return;    Print (pre[k]);    K-= pre[k]; for (int i = 0; i < n; ++i) if (K & 1 << i) puts (s[i]);}    int main () {int T, m, cost;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (int i = 0; i < n; ++i) scanf ("%s%d%d", S[i], &d[i], &c[i]);        Memset (DP, 0x3f, sizeof (DP));  Dp[0] = t[0] = 0;        All jobs on the border are not done. The deduction is divided into 0 m = 1 << n; for (int i = 1, i < m; ++i) {for (int j = 0; J < N; ++j) {if ((I & 1                << j) = = 0) continue;                int k = i-(1 << j);                T[i] = T[k] + c[j]; Cost = T[i] > d[j]?                T[i]-d[j]: 0; if (Dp[k] + cost <= dp[i])//' = ' Guaranteed dictionary order min {dp[I] = dp[k] + cost;  Pre[i] = k;        Record Path}}} printf ("%d\n", dp[m-1]);  Print (m-1); Print path} return 0;}

Doing Homework

Problem Descriptionignatius 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.
Inputthe 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.

Outputfor should output the smallest total reduced score and then give out the order of the subjects, one Subject in a line. If there is more than one orders, you should output the alphabet smallest one.

Sample Input
23Computer 3 3English 1Math 3 23Computer 3 3English 6 3Math 6 3

Sample Output
2computermathenglish3computerenglishmathHintin the second Test case, both Computer->english->math and  Computer->math->english leads to reduce 3 points, but the word "中文版" appears earlier than the word "Math", so we Choose the first Order. That is so-called alphabet order.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1074 Doing Homework (DP State compression)

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.