Week12-选做题2(状压DP)

问题描述

马上假期就要结束了,zjm还有 n 个作业,完成某个作业需要一定的时间,而且每个作业有一个截止时间,若超过截止时间,一天就要扣一分。
zjm想知道如何安排做作业,使得扣的分数最少。
Tips: 如果开始做某个作业,就必须把这个作业做完了,才能做下一个作业。

Input

有多组测试数据。第一行一个整数表示测试数据的组数
第一行一个整数 n(1<=n<=15)
接下来n行,每行一个字符串(长度不超过100) S 表示任务的名称和两个整数 D 和 C,分别表示任务的截止时间和完成任务需要的天数。
这 n 个任务是按照字符串的字典序从小到大给出。

Output

每组测试数据,输出最少扣的分数,并输出完成作业的方案,如果有多个方案,输出字典序最小的一个。

Sample input

2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3

Sample output

2
Computer
Math
English
3
Computer
English
Math

Hint

在第二个样例中,按照 Computer->English->Math 和 Computer->Math->English 的顺序完成作业,所扣的分数都是 3,由于 English 的字典序比 Math 小,故输出前一种方案。

解题思路

看到数据范围,这个题可以使用状压DP来解决。

举个例子。有五个格子_ _ _ _ _。上面可以放棋子或者不放。
我们将放棋子的格子标注为1,不放棋子的格子标注为0。
那么,我们就可以用一个二进制数来表达出人任何一个的完整状态而不是片面的,这就是状压dp。(其实就是一种暴力枚举每种方式的办法)

由于需要用到的空间是指数级别的,所以只有我们碰到这样小的数据范围,才能用状压DP。

对于这个题,枚举到状态S,如果 S = 101 1 2 S=1011_2 ,那么就代表完成了作业1,2,4。用dp[s]表示状态s的耗时情况。

我们枚举当前状态下,每个完成的作业,使得这个作业是最后完成的,然后比较计算时间,所以状态转移方程是:

dp[s]=dp[s-(1<<(x-1))]+max(cal_sum(s-(1<<(x-1)))+a[x].c-a[x].d,0);

表示当前状态下,如果第x个作业是最后完成的,那么耗时就是其他作业的耗时加上这个作业的耗时。dp[s-(1<<(x-1))]表示当前状态S去掉这个作业,其他作业的耗时。

这个题还要求在有多种情况的情况下,字典序输出结果。

对于字典序从小到大输出,我们可以在开始用字典序从大到小排序。

然后,记录每个状态下,最后完成的一个作业为pre[s]。递归回溯输出结果即可,对于相同次序的,由于字典序从大到小排序了,所以回溯输出的时候是从小到大的。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int maxn=50000;
struct node{
    string name;
    int d,c;
};
node a[16];
int t,n,dp[maxn],pre[maxn];
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int cal_sum(int _s){
    int temp=0;
    for (int i=1; i<=n; i++){
        if(_s&(1<<(i-1))) temp+=a[i].c;
    }
    return temp;
}
bool comp(const node &_a,const node &_b){
    return _a.name>_b.name;
}
void find_path(int _s){
    if(_s==0) return;

    find_path(_s-(1<<(pre[_s]-1)));

    cout<<a[pre[_s]].name<<endl;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    cin>>t;
    while(t--){
        memset(dp,0x3f3f3f,sizeof(dp)); dp[0]=0;
        cin>>n;
        for (int i=1; i<=n; i++)
            cin>>a[i].name>>a[i].d>>a[i].c;
        sort(a+1,a+1+n,comp);
        //dp[s]表示s状态下,扣的最少分数
        for (int s=1; s<=(1<<n)-1; s++){//枚举所有的状态
            for (int x=1; x<=n; x++){//枚举每个最后做的作业
                if(s&(1<<(x-1))){//当前状态做过这个作业
                    int temp=dp[s-(1<<(x-1))]+max(cal_sum(s-(1<<(x-1)))+a[x].c-a[x].d,0);
                    if(temp<dp[s]){//在当前状态s下,以x为结尾更好
                        pre[s]=x;//当前状态下,最后完成的作业是x
                        dp[s]=temp;
                    }
                }
            }
        }
        cout<<dp[(1<<n)-1]<<endl;
        find_path((1<<n)-1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43347376/article/details/106064529