State compression DP Problem

Source: Internet
Author: User

Issue: 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

Reply:

/* Analysis: For n kinds of homework, all done with the order of n!
But n! is too big, and for the completion of the work of 1,3,2 and 2,1,3 and 3,2,1 and 3,1,2
The number of days that they are consumed must be the same, but the order of completion is different so that the deduction is different.
So you can compress all the states that complete the same job into one state and record the minimum points of the clasp.
i.e.: State compression DP
For the arrival state I, from what state to reach I? Only need to enumerate all the jobs
If the job k,i contains a job K has been completed, then I can be the same status as I state is only K unfinished
The state j=i-(1<<k) to accomplish K arrives, and J must be smaller than I, if the state from 0 enumerates to 2^n-1 then J must have been computed before I
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long Long LL;
using namespace Std;

const int max= (1&LT;&LT;15) +10;
int n;
int Dp[max],t[max],pre[max],dea[20],fin[20];//dp[i] Record the minimum number of points to the status I buckle, t when the corresponding flower to spend how many days
Char s[20][110];

void output (int x) {
if (!x) return;
Output (x (1<<pre[x]));
printf ("%s\n", S[pre[x]]);
}

int main () {
int T;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i=0;i<n;++i) scanf ("%s%d%d", &s[i],&dea[i],&fin[i]);
int bit=1<<n;
for (int i=1;i<bit;++i) {//enumeration reached state I
dp[i]=inf;//Initialize the deduction of the arrival state I
for (int j=n-1;j>=0;--j) {//Because the input is entered by the character size, and each completion J is equivalent to putting J behind the completion and the following is judged to be dp[i]>dp[i-temp]+score
int temp=1<<j; So it is n-1 start, if the following judge is Dp[i]>=dp[i-temp]+score from 0 onwards
if (! ( i&temp)) continue;//status I does not exist job J completed it cannot be reached by completing the job J
int score=t[i-temp]+fin[j]-dea[j];//i-temp indicates that the state of J is not completed
if (score<0) score=0;//complete J is deducted Fraction min is 0
if (Dp[i]>dp[i-temp]+score) {
Dp[i]=dp[i-temp]+score;
t[i]=t[i-temp]+fin[j];//the time it takes to reach the state I
Pre[i]=j;//arrives at the precursor of the state I, in order to finish the work in order to final output
}
}
}
printf ("%d\n", dp[bit-1]);
Output (bit-1);//outputs the order in which jobs are completed
}
return 0;
}

State compression DP Problem

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.