zoukankan      html  css  js  c++  java
  • [HDU1074]Doing Homework (状压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
    
    
    DP姿势水平还是有待提高啊...
    对于dp[x],x的第i位为1则表示第i个作业已经完成,对于每个x枚举非零位j,如果dp[x] + (int)max(1ll*0,sumc - d[j])<dp[(1<<j)|x],说明从状态x完成j后到达
    (1<<j)|x更优,则更新。维护del数组记录每个状态最近完成的作业是哪一个,然后通过dfs输出最优状态的完成顺序。
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <cstring>
     7 #define N 20
     8 #define pow2(_) (1<<_)
     9 #define inf 0x3f3f3f3f
    10 #define D(_) cout << #_ << " " << _ << endl;
    11 using namespace std;
    12 int T,n,d[N],c[N];
    13 char s[N][105];
    14 int dp[pow2(N)],del[pow2(N)];
    15 typedef long long ll;
    16 
    17 void output(int n){
    18     if (n == pow2(del[n])) {
    19         printf("%s
    ",s[del[n]]);
    20         return;
    21     }
    22     output(n^pow2(del[n]));
    23     printf("%s
    ",s[del[n]]);
    24 }
    25 
    26 int main(){
    27     scanf("%d",&T);
    28     while(T--){
    29         scanf("%d",&n);
    30         for (int i = 0;i < n;++i){
    31             scanf("%s %d %d",&s[i],&d[i],&c[i]);
    32         }
    33         memset(dp,0x3f3f3f3f,sizeof(dp));
    34         dp[0] = 0;
    35         int maxn = (1<<n)-1;
    36         for (int i = 0;i < maxn;++i){
    37             for (int j = 0;j < n;++j){
    38                 if (pow2(j)&i) continue;
    39                 ll sumc = c[j];
    40                 for (int k = 0;k < n;++k){
    41                     if (pow2(k)&i) sumc += c[k];
    42                 }
    43                 int tmp = dp[pow2(j)|i];
    44                 dp[pow2(j)|i] = min(dp[pow2(j)|i],dp[i] + (int)max(1ll*0,sumc - d[j]));
    45                 if (tmp != dp[pow2(j)|i]) del[pow2(j)|i] = j;
    46             }
    47         }
    48 
    49         printf("%d
    ",dp[pow2(n)-1]);
    50         output(pow2(n)-1);
    51     }
    52 }


  • 相关阅读:
    重学SQL Server 笔记(二)
    H.E mapreduce操作HBase(转载)
    Paxos算法能帮助我们做什么呢? 如下几点:
    Hive入门3–Hive与HBase的整合(转载)
    Hadoop的I / O管道剖析
    install jdk in ubuntu( please notice diffrent verstion and change the name in the configuration)
    sudo bin/hadoop namenode format
    asp.net + ext grid程序示例(提供源码下载)
    摩尔定律
    it's time go to sleep ,i will be continue!
  • 原文地址:https://www.cnblogs.com/mizersy/p/12288220.html
Copyright © 2011-2022 走看看