Doing Homework
Topic abstraction: Gives the name,deadline,need of n task. Penalty time penalty=finish-deadline for each task; A task cannot be done at the same time. Asking what order to do makes penalty the smallest. Output order at the same time. If there are more than one order that satisfies the condition, the output is in dictionary order.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <stack>9#include <queue>Ten#include <Set> One#include <map> A#include <list> -#include <iomanip> -#include <cstdlib> the#include <sstream> - using namespacestd; -typedefLong LongLL; - Const intinf=0x5fffffff; + Const Doubleexp=1e-6; - Const intms= -; + A structTask at { - Charname[ the]; - intDeadline,need; - }tasks[ms]; - - structnode in { - intTime,penalty;//The time to reach this state, and the penalty time to intCur,pre;//the physical change process of state s +}nodes[1<<MS]; - the intn,m; * $ voidSolve ()Panax Notoginseng { - for(intI=1; i<=m;i++)//Note I==1 thenodes[i].penalty=INF; +nodes[0].time=0; A for(intI=0; i<=m;i++) the { + for(intj=0; j<n;j++) - if(! ((I>>J) &1)) $ { $ intnext=i| (1<<j); - intfinish=nodes[i].time+Tasks[j].need; - intPenalty=max (0, finish-tasks[j].deadline); thepenalty+=nodes[i].penalty; - if(penalty<nodes[next].penalty)Wuyi { theNodes[next].time=finish; -nodes[next].penalty=penalty; WuNodes[next].cur=J; -Nodes[next].pre=i; About } $ } - } - } - A voidOutputintx) + { the if(x==0) - return ; $ output (nodes[x].pre); theprintf"%s\n", tasks[nodes[x].cur].name); the } the the intMain () - { in intT; thescanf"%d",&T); the while(t--) About { thescanf"%d",&n); the for(intI=0; i<n;i++) thescanf"%s%d%d",tasks[i].name,&tasks[i].deadline,&tasks[i].need); +M= (1<<n)-1; - solve (); theprintf"%d\n", nodes[m].penalty);Bayi output (m); the } the return 0; -}
Doing Homework State Compression DP