其他分享
首页 > 其他分享> > Doing Homework HDU - 1074 (状压dp)

Doing Homework HDU - 1074 (状压dp)

作者:互联网

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

InputThe input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework). 

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier. 
OutputFor each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one. 
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

In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the 
word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.

题意:
有n个作业,每个作业有一个截止日期和一个完成所需时间,如果一个作业迟交x天,就要扣除x个学分,求扣除作业最小的写作业顺序,如果多个方案,输出字典序最小的那个。
思路:
状压dp
dp[i][j]:j是状压后的数字,二进制位上,1表示以完成,0表示未完成。i表示i是j状态最后一个完成的作业。
用一个pre数组记录完成作业的顺序。
状态转移方程请详见代码。
此题的要点是在j状态下,dp数组增加一维i来记录最后一个完成的作业,用以记录作业完成的顺序。
当然其他博主有一维的写法,不过私以为我的代码更容易理解,不过时间复杂度也多了一个n。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int n;
char name[18][108];
int d[20],c[20];
int dp[20][33000];
int f[33000];
struct node{
    int x,y;
};
node pre[20][33000];
stack<int>st;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s%d%d",name[i],&d[i],&c[i]);
        }
        memset(dp,0x3f,sizeof(dp));
        memset(f,0,sizeof(f));
        memset(pre,-1,sizeof(pre));

        dp[0][0]=0;
        int m=(1<<n)-1;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(i&(1<<j)){continue;}
                int k=i|(1<<j);
                f[k]=f[i]+c[j];
                for(int t=0;t<n;t++){
                    if(dp[j][k]>=dp[t][i]+max(0,f[k]-d[j])){
                        dp[j][k]=dp[t][i]+max(0,f[k]-d[j]);
                        pre[j][k]=node{t,i};
                    }
                }
            }
        }
        int ans=inf,s;
        for(int i=0;i<n;i++){
            if(ans>=dp[i][m]){ans=dp[i][m];s=i;}
        }
        printf("%d\n",ans);
        node exa=node{s,m};
        while(true){
            if(exa.y==0){break;}
            st.push(exa.x);
            exa=pre[exa.x][exa.y];
        }
        while(!st.empty()){
            printf("%s\n",name[st.top()]);
            st.pop();
        }
    }
    return 0;
}
View Code

 

标签:HDU,Doing,1074,Computer,Math,English,test,include,dp
来源: https://www.cnblogs.com/ZGQblogs/p/10584778.html