zoukankan      html  css  js  c++  java
  • HDU 1074:Doing Homework(状压DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1074

    Doing Homework

    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
    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门课程的作业需要完成,每科作业都有一个deadline和一个消耗的时间,如果完成该门课程的作业的时间超过deadline,则每超过一天就会扣一分,求如何安排完成作业的次序才能使得完成所有作业扣的分数最少,最少分数是多少,并输出做作业的次序。
    思路:第一道状压DP。一开始傻傻地想用贪心去做,后来发现不可行,想了挺久才看题解,原来是忌惮了很久的状压DP,因为有挺多的位运算搞不太懂,然后自己恶补了一下这方面知识,算作模模糊糊地搞懂了吧。因为N很小,可以枚举所有情况。5可以二进制表示为1001,从右往左数,该状态表示完成了第一门和第四门作业,就是用1或者0来表示该点有没有经过,然后枚举1<<i(i<N)就相当于枚举每一科的作业, now&i == 0 表示第i+1门作业没完成,now是已经完成了的作业的集合。就这样可以进行DP求解了。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <string>
     5 #include <map>
     6 #include <stack>
     7 #include <iostream>
     8 using namespace std;
     9 #define N 15
    10 #define INF 100000000
    11 struct node
    12 {
    13     int dead,cost;
    14     char s[105];
    15 }course[N+1];
    16 struct P
    17 {
    18     int pre,score,now,time;
    19 //pre记录完成某一个学科的作业之前已经完成的学科的作业的集合
    20 //now记录当前更新的学科,pre和now用作路径输出
    21 //score是减少的学分的总数,time是当前时间
    22 }dp[1<<N];
    23 int vis[1<<N];
    24 
    25 int main()
    26 {
    27     int t;
    28     cin>>t;
    29     while(t--){
    30         int n;
    31         cin>>n;
    32         memset(dp,0,sizeof(dp));
    33         memset(vis,0,sizeof(vis));
    34         for(int i=0;i<n;i++){
    35             cin>>course[i].s>>course[i].dead>>course[i].cost;
    36         }
    37         int en=(1<<n)-1;
    38         for(int i=0;i<en;i++){
    39             for(int j=0;j<n;j++){
    40                 int temp=1<<j;
    41                 if((i&temp)==0){//如果当前的学科作业还没完成
    42                     int cur=i|temp;//当完成该科作业时的情况
    43                     dp[cur].time=dp[i].time+course[j].cost;//当前使用的时间
    44                     int b=dp[i].time+course[j].cost-course[j].dead;
    45                     if(b<0) b=0;
    46                     if(vis[cur]){//如果已经完成过该作业就更新
    47                         if(dp[cur].score>dp[i].score+b){
    48                             dp[cur].score=dp[i].score+b;
    49                             dp[cur].pre=i;//还没完成该学科之前已经完成了的学科
    50                             dp[cur].now=j;//当前的学科
    51                         }
    52                     }
    53                     else{//如果没完成直接更新
    54                         vis[cur]=1;
    55                         dp[cur].score=dp[i].score+b;
    56                         dp[cur].pre=i;
    57                         dp[cur].now=j;
    58                     }
    59                 }
    60             }
    61         }
    62         cout<<dp[en].score<<endl;
    63         stack <int> S;
    64         //用栈输出完成作业的路径,也可以用递归
    65         while(en>0){
    66             S.push(dp[en].now);
    67             en=dp[en].pre;
    68         }
    69         while(!S.empty()){
    70             cout<<course[S.top()].s<<endl;
    71             S.pop();
    72         }
    73     }
    74     return 0;
    75 }

    2016-06-26

     
  • 相关阅读:
    Django的模型层(1)- 单表操作(上)
    Django-1版本的路由层、Django的视图层和模板层
    Django准备知识-web应用、http协议、web框架、Django简介
    MySQL数据库(5)- pymysql的使用、索引
    MySQL数据库(4)- 多表查询、可视化工具Navicat的使用、设计模式MVC
    MySQL练习题
    MySQL数据库(3)- 完整性约束、外键的变种、单表查询
    MySQL数据库(2)- 库的操作、表的操作、数据的操作、存储引擎的介绍
    MySQL数据库(1)- 数据库概述、MySQL的安装与配置、初始SQL语句、MySQL创建用户和授权
    前端收尾
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5618332.html
Copyright © 2011-2022 走看看