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;}