zoukankan      html  css  js  c++  java
  • [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.
     
    题意:
      小明的作业又写不完了,总共有N个科目,给出每个科目的截止日期和写完该科目所需要的时间,如果逾期一天就扣一点,问他应该怎样安排使所扣点数最少。输入和输出均按照字典序。
    思路:
      作业的数量不多,最多15们,可以用状态压缩,如101表示做第1和第3门的状态,1为做,0为不做,111可由110 101 011转移而来  设dp[i]为状态为i时的最小点数。
      状态转移方程:dp[i] = min(dp[k] + cost)   k为i的所以上一阶段 如上面的110 101 011是111的上一阶段。
    用位运算来完成相关操作:
      i & (1<<j) 取i的第j位    k = (i - (1<<j) 移除i的第j位 保留其他位
    代码:
    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define INF 0x3f3f3f3f
    const int msta = 1<<16;
    const int msub = 16;
    
    char sub[msub][110];
    int d[msub], cost[msub];
    int t[msta], pre[msta];
    int dp[msta];
    int N;
    
    void Print(int k)
    {
        if (k == 0) return;
        Print(pre[k]);
        k = k-pre[k];
        for (int i = 0; i < N; i++) {
            if ((k & (1<<i)))
                printf("%s
    ", sub[i]);
        }
    }
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        int T;
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &N);
            for (int i = 0; i < N; i++) {
                scanf("%s%d%d", sub[i], &d[i], &cost[i]);
            }
            memset(dp, 0x3f, sizeof(dp));
            dp[0] = t[0] = 0;
            int M = (1 << N);
            for (int i = 0; i < M; i++) {
                for (int j = 0; j < N; j++) {
                    if ((i & (1<<j)) == 0) continue;
                    int k = (i - (1<<j));
                    t[i] = t[k] + cost[j];
                    int rs = d[j]-t[i] > 0 ? 0 : t[i]-d[j];
                    if (dp[k] + rs <= dp[i]) {  //=保证了字典序 
                        dp[i] = dp[k] + rs;
                        pre[i] = k;
                    }
                }
            }
            printf("%d
    ", dp[M-1]);
            Print(M-1);
        }
    
        return 0;
    }
  • 相关阅读:
    五、K3 WISE 开发插件《直接SQL报表开发新手指导
    四、K3 WISE 开发插件《工业单据老单插件开发新手指导》
    三、K3 WISE 开发插件《K3 WISE开发手册》
    二、K3 WISE 开发插件《 工业单据老单客户端插件事件、属性、方法》
    一、K3 WISE 开发插件《K3 WISE常用数据表整理》
    首次安装金蝶注意事项
    金蝶系统,反写采购价格管理资料状态怎么选择?
    网络编程----socket套接字的黏包问题
    网络编程----socket套接字
    网络编程----网络协议篇(osi七层协议)
  • 原文地址:https://www.cnblogs.com/whileskies/p/7298134.html
Copyright © 2011-2022 走看看