Question:
Deadline that provides the time required to complete n homework and the time required to hand in N homework.
If you pay a day later than deadline, you will be deducted a point.
The order of completion is arranged to minimize the number of points deducted.
Train of Thought Analysis:
DP [s] indicates that the optimal score deducted from homework in S state is completed.
For each status, we add each unfinished course to the course during the transfer, which ensures that the course is completed one by one.
Note that the smallest Lexicographic Order is the problem. In the beginning, the input strings are sorted from small to large, and each time they are added to the State from small to large.
In this way, the minimum Lexicographic Order of the added status can be ensured.
But also the output scheme, so the ANS array saves the lessons added to the previous state and current state.
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <set>#include <vector>#include <map>#include <string>#define maxn 1005using namespace std;int dp[1<<15];int ans[1<<15][2];int time[1<<15];struct node{ string str; int dead,cost; bool operator < (const node &cmp)const { return str<cmp.str; }}a[20];void print(int s){ if(s==0)return; print(ans[s][0]); cout<<a[ans[s][1]].str<<endl;}int main(){ int T; cin>>T; while(T--) { int n; cin>>n; for(int i=0;i<n;i++) cin>>a[i].str>>a[i].dead>>a[i].cost; sort(a,a+n); memset(dp,0x3f,sizeof dp); memset(time,0,sizeof time); memset(ans,0,sizeof ans); dp[0]=0; for(int s=0;s<(1<<n);s++) { for(int i=0;i<n;i++) { if((1<<i)&s)continue; int c=max(0,time[s]+a[i].cost-a[i].dead); if(dp[s]+c < dp[s|(1<<i)]) { dp[s|(1<<i)]=dp[s]+c; time[s|(1<<i)]=time[s]+a[i].cost; ans[s|(1<<i)][0]=s; ans[s|(1<<i)][1]=i; } } } cout<<dp[(1<<n)-1]<<endl; print(ans[(1<<n)-1][0]); cout<<a[ans[(1<<n)-1][1]].str<<endl; } return 0;}