• HDU1074 Doing Homework —— 状压DP


    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074

    Doing Homework

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


    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
     
     
     
     
    代码如下:
     1  #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 15+10;
    19 
    20 struct node
    21 {
    22     string name;
    23     int deadline, cost;
    24 }subject[MAXN];
    25 int dp[1<<16], id[1<<16], pre[1<<16];
    26 //id[status]表示当前的状态是由添加哪一科而得来的, id[status]的值即subject数组的下标。
    27 //pre[status]表示当前的状态是由哪一个状态转移过来的。
    28 
    29 void Print(int s)
    30 {
    31     if(!s) return;
    32     Print(pre[s]);  //追溯上一个状态
    33     cout<< subject[id[s]].name <<endl;
    34 }
    35 
    36 int main()
    37 {
    38     int T, n;
    39     scanf("%d", &T);
    40     while(T--)
    41     {
    42         scanf("%d", &n);
    43         for(int i = 0; i<n; i++)
    44             cin>>subject[i].name>>subject[i].deadline>>subject[i].cost;
    45 
    46         memset(dp, -1, sizeof(dp));
    47         dp[0] = 0;
    48         for(int s = 0; s<(1<<n); s++)
    49         {
    50             int time = 0;
    51             for(int i = 0; i<n; i++)    //计算当前时间
    52                 if(s&(1<<i))
    53                     time += subject[i].cost;
    54 
    55             for(int i = 0; i<n; i++)
    56             {
    57                 if(!(s&(1<<i)))
    58                 {
    59                     int exceeded = time + subject[i].cost - subject[i].deadline;    //计算超出时间
    60                     if(exceeded<0) exceeded = 0;
    61                     int tmp = s|(1<<i);
    62                     if(dp[tmp]==-1 || dp[tmp]>dp[s]+exceeded)   //更新
    63                     {
    64                         dp[tmp] = dp[s] + exceeded;
    65                         id[tmp] = i;
    66                         pre[tmp] = s;
    67                     }
    68                 }
    69             }
    70         }
    71 
    72         printf("%d
    ", dp[(1<<n)-1]);
    73         Print((1<<n)-1);
    74     }
    75 }
    View Code
  • 相关阅读:
    week8
    2020中国大学生程序设计竞赛(CCPC)-网络选拔赛 题解
    卷积形式dp的多项式求逆做法
    多项式乘法逆(review)
    LaTex学习
    BZOJ 2653 middle
    BZOJ3207 花神的嘲讽计划Ⅰ
    BZOJ1901 Zju2112 Dynamic Rankings
    POJ2104 K-th Number
    平衡树总结专题
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7631252.html
Copyright © 2020-2023  润新知