say_c_box的博客

只是为了记录

HDU1074——Doing Homework(状态压缩dp)

Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9323    Accepted Submission(s): 4405


Problem Description
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.
 

Input
The 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.
 

Output
For 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.
 

Author
Ignatius.L
 

Recommend
简单状态压缩dp



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

#define _ ios::sync_with_stdio(false)
typedef long long ll;
const int MAXN=20;
const int INF=1e9+7;

struct hom{
    string name;
    int d,t;
}a[MAXN];
int cmp(hom x,hom y){
    return x.name>y.name;
}

int dp[1<<20];
int sum[1<<20];
int pre[1<<20];

void dfs(int s){
    if(s==0)
        return;
    int x=pre[s];
    int temp=s&(~(1<<x));
    dfs(temp);
    cout<<a[pre[s]].name<<endl;
}
int main(){
    _;
    int T;
    cin>>T;
    while(T--){
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i].name>>a[i].d>>a[i].t;
        }
        sort(a,a+n,cmp);
        memset(sum,0,sizeof(sum));
        for(int i=0;i<1<<n;i++){
            dp[i]=INF;
            for(int j=0;j<n;j++){
                int temp=1<<j;
                if(i&temp){
                    sum[i]+=a[j].t;
                }
            }
        }
        dp[0]=0;
        for(int i=0;i<1<<n;i++){
            for(int j=0;j<n;j++){
                int temp=(1<<j);
                if(temp&i){
                    int x=i&(~temp);
                    if(sum[x]+a[j].t>a[j].d){
                        if(dp[x]+sum[x]+a[j].t-a[j].d<dp[i]){
                            pre[i]=j;
                            dp[i]=dp[x]+sum[x]+a[j].t-a[j].d;
                        }
                    }else{
                        if(dp[i]>dp[x]){
                            pre[i]=j;
                            dp[i]=dp[x];
                        }
                    }
                }
            }
        }
        int temp=((1<<n)-1);
        cout<<dp[temp]<<endl;
        dfs(temp);
    }
}









阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/say_c_box/article/details/74157539
文章标签: 压缩 dp 算法
个人分类: 算法 动态规划
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭