! HDU 1074 Doing homework--dp--(state compression)

Source: Internet
Author: User

Test instructions: N jobs, each job has deadline and the time to complete the job cost, the completion of the job every more than a day, minus one point, to subtract the smallest score

Analysis: The optimal solution is taken in the whole arrangement of the job, but 15! Too big will time out, so with the binary to state compression, 15 bits, the 0/1 of the first bit indicates whether the first job is completed.

1. State compression is used

2. Saving and outputting the optimal solution sequence method

Slowly deepen your understanding.

Code:

#include <iostream> #include <cstring> #include <string> #define INF 1<<16using namespace std; int t,n;struct node1{string chr;int ddl,cost;} A[16];struct node2{int Pre,reduced,days;} Dp[inf];int vis[inf];void Output (int i) {int tmp=dp[i].pre^i;int cnt=0;tmp>>=1;while (tmp) {cnt++;tmp>>=1;} if (dp[i].pre!=0) Output (dp[i].pre); Cout<<a[cnt].chr<<endl;} int main () {cin>>t;while (t--) {memset (vis,0,sizeof (Vis)); cin>>n;for (int i=0;i<n;i++) Cin>>a[i] . Chr>>a[i].ddl>>a[i].cost;dp[0].pre=-1,dp[0].days=0,dp[0].reduced=0;vis[0]=1;int en= (1<<n)-1; for (int i=0;i<en;i++) {for (int j=0;j<n;j++) {int tmp=1<<j;if ((i&tmp) ==0) {int Tmp2=i|tmp;int day=dp[i] . Days+a[j].cost;dp[tmp2].days=day;int reduce=day-a[j].ddl;if (reduce<0) reduce=0;reduce+=dp[i].reduced;if (vis[ TMP2]) {if (reduce<dp[tmp2].reduced) {dp[tmp2].reduced=reduce;dp[tmp2].pre=i;}} else{vis[tmp2]=1;dp[tmp2].reduced=reduce;dp[tmp2].pre=i;}}}} Cout<<dp[en].rEduced<<endl;output (en);}} 


! 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.