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)