• HDU 1074 Doing Homework


    Doing Homework

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


    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.

    题目大意是知道几门课的最后的期限和花费时间,如果超过期限完成作业需要扣分,求最后以怎么样的顺序完成所有作业扣分最少并输出其花费时间完成顺序。如果相等的情况以字典徐输出。

    一个状态压缩的题目,以进制的形式表示完成的作业的状态,比如三个作业的话,有2^3种状态,假设顺序是Computer,English,Math。有000表示三个都没有完成,001表示只完成了数学,011表示完后了数学和英语

    这样就可以从0到1 << n取所有的状态,并进行DP。dp[t]代表t时候的状态。要得到此状态,可以从0到n分别遍历是否有i使得i代表的科目与该状态有交集,即(t & (1 << i))。如果有交集,那么dp[t] 可以从dp[t -i]来获得,这样就得到了转移方程。

    为了方便输出,需要在dp数组里面存储当前和上一个结点的信息,最后入栈出栈解决。

    代码如下:

    /*************************************************************************
    	> File Name: Doing_Homework.cpp
    	> Author: Zhanghaoran
    	> Mail: chilumanxi@xiyoulinux.org
    	> Created Time: Fri 23 Oct 2015 02:01:24 PM CST
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <stack>
    using namespace std;
    const int INF = 1 << 29;
    struct Node{
        char str[100];
        int deadline;
        int costtime;
    }a[20];
    int T;
    
    struct NOde{
        int pre;
        int now;
        int cost;
        int tim;
    }dp[1 << 15];
    int main(void){
        scanf("%d", &T);
        while(T --){
            memset(dp, 0, sizeof(dp));
            int n ;
            scanf("%d", &n);
            for(int i = 0; i < n; i ++){
                scanf("%s", a[i].str);
                scanf("%d%d", &a[i].deadline, &a[i].costtime);
            }
            for(int t = 1; t < (1 << n); t ++){          //t是所有的状态。
                dp[t].cost = INF;                        //初始化花费
                for(int i = n - 1; i >= 0; i --){        //便利找已经有交集的
                    int temp = 1 << i;
                    if(!(t & temp))
                        continue;
                    int past = t - temp;                 //如果找到转移需要依赖前一个状态。
                    int cst = dp[past].tim + a[i].costtime - a[i].deadline; //记录按照上一个状态到当前的状态的花费
                    cst = cst < 0 ? 0 : cst;
                    if(cst + dp[past].cost < dp[t].cost){  //如果花费变少需要更新时间和信息
                        dp[t].cost = cst + dp[past].cost;
                        dp[t].now = i;
                        dp[t].pre = past;
                        dp[t].tim = dp[past].tim + a[i].costtime;
                    }
                }
            }
    
            stack<int> Stack;                            //入栈退栈输出
            int temp = (1 << n) - 1;
            printf("%d
    ", dp[temp].cost);
            while(temp){
                Stack.push(dp[temp].now);
                temp = dp[temp].pre;
            }
            while(!Stack.empty()){
                printf("%s
    ", a[Stack.top()].str);
                Stack.pop();
            }
        }
    }


  • 相关阅读:
    Swift之类型安全和类型推断
    Swift之浮点数
    Swift之整数
    泛互联网产品技术支持划分
    WPF WebBrowser 不可见问题的解析[转]
    form WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
    Mvvm绑定datagrid或listview的selectItems的方法[转]
    [C#]获取最近在Windows上所使用的文件
    拷贝构造函数和赋值运算符重载的区别
    Why Doesn’t Drag-and-Drop work when my Application is Running Elevated? – A: Mandatory Integrity Control and UIPI(转载)
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136076.html
Copyright © 2020-2023  润新知