HDU 1074-doing Homework (pressure DP)

Source: Internet
Author: User

Topic Link: Click to open the link

Did a long time. Start with a search, write, write, think 15! How to prune the factorial again seems to be too much. In particular, when the dictionary sequence is not good processing AH later asked the Flying God is like pressure dp. Sad didn't know what it was like to be a pressure.

Test instructions: There are n homework to give each of the time and the completion of the work required to complete the operation of the optimal order, so that the minimum deduction (over the period to deduct points)

Idea: The completion of each job to see the status of 2, binary from right to left once the corresponding Job 1 2 ... n, the corresponding bit is 1 to indicate that the corresponding job is completed, and vice versa. So the general state has 2^n-1 species,

Dp[2^n-1] On behalf of all the work completed, Dp[0] on behalf of the work has not been completed. The DP array is open to the struct type, Dp[i] saves the current subtraction, the prefix, and the current time. Recursion from 0. Recursive printing. Most of these operations involve in-place operations, at first glance is very mindless, look at the definition of bit operations just fine

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1<<16#define _ll __int64# Define ll long long#define INF 0x3f3f3f3f#define Mod 100000000#define pp pair<int,int> #define ull unsigned long long Using namespace Std;struct node{int cost,re,pre;} Dp[maxn];char s[16][106];int n,c[16],d[16];bool vis[maxn];void init () {Dp[0].cost=0;dp[0].pre=-1;dp[0].re=0;memset ( Vis,0,sizeof (Vis));} void output (int sb) {int cur=dp[sb].pre^sb,cnt=0;while (cur) {cnt++;cur>>=1;} if (dp[sb].pre!=0) output (dp[sb].pre);p rintf ("%s\n", s[cnt]);} void Solve () {int tot= (1<<n) -1;for (int i=0;i<tot;i++) {for (int j=1;j<=n;j++) {int cur=1<< (j-1); Cur&i)) {int Tem=cur|i;dp[tem].cost=dp[i].cost+c[j];int Reduce=dp[tem].cost<d[j]?0:dp[tem].cosT-d[j];reduce+=dp[i].re;if (vis[tem]&&reduce<dp[tem].re) {dp[tem].re=reduce;dp[tem].pre=i;} if (!vis[tem]) {vis[tem]=1;dp[tem].re=reduce;dp[tem].pre=i;}}}} printf ("%d\n", dp[tot].re); output (tot);} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (int i=1;i<=n;i++) scanf ("%s%d%d", s[i], &d[i],&c[i]); init (); Solve ();} return 0;}



HDU 1074-doing Homework (pressure DP)

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.