HDU 1074

时间:2023-03-08 21:52:37

http://acm.hdu.edu.cn/showproblem.php?pid=1074

每个任务有一个截止日期和完成时间,超过截止日期一天扣一分,问完成全部任务最少扣几分,并输出路径

最多15个任务,状态压缩一下进行dp,输出路径的话要记录每种状态的前驱,存起来逆序输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std; const int INF = 0xfffffff; struct fuck{
int w, pre;
}dp[<<]; struct node{
char name[];
int D, C;
}p[]; int cal(int x){
int res = , cnt = ;
while(x){
if(x & ){
res += p[cnt].C;
}
cnt++;
x >>= ;
}
return res;
} int cnt(int x){
int res = ;
while(x){
if(x & ){
res++;
}
x >>= ;
}
return res;
} int cal1(int x, int y){
int res = ;
while(){
if((x&) != (y&)){
return res;
}
res++;
x >>= ; y >>= ;
}
} int ans[], ct; int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = ; i < (<<n); i++)
dp[i].w = INF;
int xx = ;
while(){
dp[(<<xx)].pre = ;
xx++;
if(xx == n) break;
}
for(int i = ;i < n; i++){
scanf("%s %d %d", p[i].name, &p[i].D, &p[i].C);
if(p[i].C > p[i].D)
dp[<<i].w = p[i].C - p[i].D;
else
dp[<<i].w = ;
}
map <int, int> mp;
for(int i = ; i < (<<n); i++){
for(int j = ; j < n; j++){
if(i&(<<j)){
if(cal(i) > p[j].D){
if(dp[i].w > dp[i^(<<j)].w + cal(i) - p[j].D){
dp[i].w = dp[i^(<<j)].w + cal(i) - p[j].D;
dp[i].pre = i^(<<j);
mp[i] = i^(<<j);
}
else if(dp[i].w == dp[i^(<<j)].w + cal(i) - p[j].D && dp[i].pre > (i^(<<j))){
dp[i].pre = i^(<<j);
mp[i] = i^(<<j);
}
//dp[i] = min(dp[i], dp[i^(1<<j)] + cal(i) - p[j].D);
}
else{
if(dp[i].w > dp[i^(<<j)].w){
dp[i].w = dp[i^(<<j)].w;
dp[i].pre = i^(<<j);
mp[i] = i^(<<j);
}
else if(dp[i].w == dp[i^(<<j)].w && dp[i].pre > (i^(<<j))){
dp[i].pre = i^(<<j);
mp[i] = i^(<<j);
}
//dp[i] = min(dp[i], dp[i^(1<<j)]);
}
}
}
}
printf("%d\n", dp[(<<n)-]);
int now = (<<n) - ;
ct = ;
while(now){
ans[ct++] = cal1(now, mp[now]);
now = mp[now];
}
for(int i = n - ; i >= ; i--){
printf("%s\n", p[ans[i]].name);
}
}
return ;
}