HDU 1074 doing homework (status DP)

Source: Internet
Author: User

Question link: Click the open link

This is a relatively simple state DP with a data volume of 15, so basically there is no need to suspect that this is a state DP problem.

This question can reflect the essence of status DP.

First, bitwise operations are used to determine whether the current status can be obtained through the transfer of those statuses.

There is also a path record

This piece of code: I = (I &(~ (1 <DP [I]. Val )));

The current status is from DP [I]. if Val is converted, the job that converts the status will use the code above, that is, the value of Val 1 to 0.

#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;#define maxn 999999999struct point{    char name[101];    int deadline;    int spend;}po[20];struct node{    int val;    int last;    int time;}dp[1<<15+10];int n;int ans[20];int main(){    int t,i,j,k,p;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=0;i<n;i++)        scanf("%s%d%d",po[i].name,&po[i].deadline,&po[i].spend);        for(i=0;i<(1<<n);i++)        dp[i].time=maxn,dp[i].val=-1,dp[i].last=0;        dp[0].time=0;        for(i=0;i<(1<<n);i++)        for(j=0;j<n;j++)        {             if ((i & (1<<j)))             continue;                p=(i | (1<<j));                 if (dp[p].time>dp[i].time+max(dp[p].last-po[j].deadline,0))                {                    dp[p].last=dp[i].last+po[j].spend;                    dp[p].time=dp[i].time+max(dp[p].last-po[j].deadline,0);                    dp[p].val=j;                }        }        printf("%d\n",dp[(1<<n)-1].time);        i=(1<<n)-1;        k=0;        while(dp[i].val!=-1)        {            ans[k++]=dp[i].val;            i=(i & (~(1<<dp[i].val)));        }        for(k--;k>=0;k--)        printf("%s\n",po[ans[k]].name);    }    return 0;}

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.