hdu-1074-Doing Homework-状态压缩dp-java

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

解题思路:
                这是我状态压缩的第一道题 emm 本题是压缩作业的全排列做还是不做 对应的就是01字符串 也就是一个十进制数
               最后求的是 n个1组成的二进制状态 也就是所有作业都完成 也就是求dp[2^n-1] 下面是代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class hdu1074 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);
        int q = scanner.nextInt();
        for (int p = 0; p < q; p++) {
            int n = scanner.nextInt();
            HomeWork [] subs = new HomeWork[n];
            for (int i = 0; i < n; i++) {
                subs[i] =  new HomeWork(scanner.next(), scanner.nextInt(), scanner.nextInt());
            }
            int max = (int) (Math.pow(2, n) - 1);
            Node [] dp = new Node[max+1];
            dp[0] = new Node(0, 0, ""); //dp[0]表示一个作业都没做
            for (int i = 0; i < dp.length; i++) { //第一层循环 就是枚举所有 的01排列
                for (int j = 0; j < n; j++) { //第二层循环 枚举n门作业 用来计算 我在当前状态做了第j门作业 后跳转到另外一个状态的最优值
                    int indexWork = 1 << j;  //左移j位 模拟第j门作业在二进制串中的位置
                    if ((i & indexWork) == 0) { //判断当前的状态能不能做第j门作业
                        int later = i | indexWork; //如果能作第j门作业 那么跳转到的状态是later
                        if (dp[later] == null) { //如果跳转的状态没值 直接赋值就可以了
                            int t = dp[i].t + subs[j].time;
                            int o = dp[i].o + (t > subs[j].deadline ? t - subs[j].deadline : 0);
                            dp[later] = new Node(t, o, dp[i].w+subs[j].subject+" ");
                        }else {//如果有值 进行比较找到罚时最小的然后更新
                            int t = dp[i].t + subs[j].time;
                            int o = dp[i].o + (t > subs[j].deadline ? t - subs[j].deadline : 0);
                            if (o < dp[later].o) {
                                dp[later] = new Node(t, o, dp[i].w+subs[j].subject+" ");
                            }
                        }
                    }
                }
            }
            System.out.println(dp[max].o);
            String [] sum = dp[max].w.split(" ");
            for (int i = 0; i < sum.length; i++) {
                System.out.println(sum[i]);
            }
        }
    }

}
//dp点
class Node {
    int t; //总用时
    int o; //罚时
    String w; //路径
    public Node(int t, int o, String w) {
        // TODO Auto-generated constructor stub
        this.t = t;
        this.o = o;
        this.w = w;
    }
}
//作业
class HomeWork{
    String subject; //学科
    int deadline;   //截止时间
    int time;       //完成时间
    public HomeWork(String subject, int deadline, int time) {
        // TODO Auto-generated constructor stub
        this.subject = subject;
        this.deadline = deadline;
        this.time = time;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值