HDU 1074 Doing Homework(状压DP)

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

第一道状态压缩DP.状压DP思想就是利用二进制(好多都用)的 01表示某种状态,在本题中就是某门课程做没做。

在递推关系的时候对于DP[i]看它是否包含某种作业,即能否通过做某种作业来达到dp[i].感谢:点击打开链接

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int maxn=1<<15;
char s[20][110];
int dp[maxn],t[maxn],pre[maxn];//dp[i]储存做作业的各种状态,t[i]表示经过的时间,pre[i]表示某种状态的的前驱
int dead[20],fin[20];
void print(int x)
{
   if(!x)
      return ;
   print(x-(1<<pre[x]));
   printf("%s\n",s[pre[x]]);
}
int main()
{
    int tt,n;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d",&n);
        memset(t,0,sizeof(t));
        memset(pre,0,sizeof(pre));
        for(int i=0;i<n;i++)
            scanf("%s%d%d",&s[i],&dead[i],&fin[i]);
        int end=1<<n;
        for(int i=1;i<end;i++)
        {
            dp[i]=INT_MAX;
            for(int j=n-1;j>=0;j--)//从后往前枚举
            {
                int temp=1<<j;
                if(!(i&temp))
                    continue;
                int cost=t[i-temp]+fin[j]-dead[j];//时间消耗
                if(cost<0)
                    cost=0;
                if(dp[i]>dp[i-temp]+cost)
                {
                    dp[i]=dp[i-temp]+cost;
                    t[i]=t[i-temp]+fin[j];
                    pre[i]=j;
//                    cout<<i<<" "<<j<<endl;
                }
            }
        }
        printf("%d\n",dp[end-1]);//end-1是每种作业都完成的状态
        print(end-1);
    }
    return 0;
}

HDU 1074 Doing Homework(状压DP)

时间: 2025-01-15 16:56:03

HDU 1074 Doing Homework(状压DP)的相关文章

HDU 1074 Doing Homework 状压DP

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 r

HDU 4924 Football Manager(状压DP)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4924 题意 : n(<=20)个人选出11个人作为首发组成a-b-c阵容,每个人都有自己擅长的位置,并且每个人和其他人会有单向的厌恶和喜欢关系,每个人对于自己擅长的位置都有两个值CA和PA,有喜欢或者厌恶关系的两个人在一起也会影响整个首发的CA总值,要求选出一套阵容使得CA最大,或者CA一样的情况下PA最大. 思路 : 状压搞,dp[s]s的二进制表示20个人中选了那几个人,然后规定选进来的顺序

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

HDU 5765 Bonds(状压DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不同的连通块,对于每条边,求出两边的联通块的划分方案数,就是对于该点的答案. [代码] #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int n,m,T,Cas=1

hdu 4778 Gems Fight! 状压dp

转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得到的最大值 代码也来自wdd 1 /****************************************************** 2 * File Name: b.cpp 3 * Author: kojimai 4 * Creater Time:2014年08月13日 星期三 11时

HDU 1074 Doing Homework(像缩进DP)

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 r

HDU 3001 Travelling (状压DP,3进制)

题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示,3进制可以先将表打出来.在走的时候注意只能走2次,其他的和普通的TSP状压DP是一样的.注意点:重边,自环等等,老梗了. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #i

HDU 4628 Pieces(状压DP)题解

题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorithm&