• HDU-1047(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

    思路:
    这题是很值的花时间好好理解的一道题目,当时就是把这题给搞明白了,才对DP有了真正意义上的认识
    将所有的状态非常直观的转化成二进制数列,然后分别求”当前状态的最大值“,这一途径是通过遍历所有能构成当前状态的”上一状态“实现的
    这样就很直观的展现了DP的思想,即当前的”最优解“是通过枚举所有能达到当前状态的”上一状态“的解来实现。

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<stack>
    using namespace std;
    
    #define N 15
    
    typedef struct
    {
     char name[101];
     int deadline;
     int cost;
    }Course;
    
    typedef struct
    {
     int timeUsed;  //到此状态总共花费的时间
     int minPoint;  //此状态最小的被罚分数
     int preState;  //此状态对应的前一状态
     int lastCourse;  //从上一状态转移到此状态完成的course
    }State;
    
    Course homework[16];
    State states[1<<N];
    int n; //课程数
    
    void outputResult()
    {
     stack<int> s;
     int tempState;
     int tempCourse;
     char courseName;
    
     tempState = (1<<n) - 1;
     printf("%d
    ", states[tempState].minPoint);
     while(tempState != 0)
     {
      tempCourse = states[tempState].lastCourse;
      s.push(tempCourse);
      tempState = states[tempState].preState;
     }
    
     while(!s.empty())
     {
      tempCourse = s.top();
      s.pop();
      printf("%s
    ", homework[tempCourse].name);
     }
    }
    
    void processDP()
    {
     int i, j, maxStateN;
     int temp;
     int reduce;
     int preState;
    
     states[0].lastCourse = 0;
     states[0].minPoint = 0;
     states[0].preState = -1;
     states[0].timeUsed = 0;
    
     maxStateN = 1<<n;
    
     for(i=1; i<maxStateN; i++)
     {
      states[i].minPoint = 0x7fffffff;
      for(j=n-1; j>=0; j--)  //这里j从n-1递减到0,是为了保证当罚分一样时,顺序按照字母顺序递增的方式排列
      {
       temp = 1<<j;
       if(i & temp) //状态i中包含第j门课
       {
    
        preState = i - temp;
        reduce = states[preState].timeUsed+homework[j].cost-homework[j].deadline;
        if(reduce < 0)
        {
         reduce = 0;
        }
       
        if(states[i].minPoint > states[preState].minPoint + reduce)
        {
         states[i].minPoint = states[preState].minPoint + reduce;
         states[i].lastCourse = j;
         states[i].preState = preState;
         states[i].timeUsed = states[preState].timeUsed + homework[j].cost;
        }
       }
      }
     }
    }
    
    int main()
    {
     int i;
     int T;
    
     scanf("%d", &T);
     while(T--)
     {
      scanf("%d", &n);
      for(i=0; i<n; i++)
      {
       scanf("%s%d%d", homework[i].name, &homework[i].deadline, &homework[i].cost);
      }
    
      processDP();
      outputResult();
     }
     return 0;
    }

  • 相关阅读:
    linux下给U盘分区&制作文件系统
    迭代器 配接器
    仿函数
    在查询用户的权限的时候 使用左外连接 和 access数据库中左外连接
    C# 想要程序文件移动 而数据保持相对位置
    C# 第三方控件 下面的Item不显示了
    C# 第三方控件 错误 LC-1
    c# 第三方控件 闪退
    access 语句错误
    poj 1469(二分图 最大匹配)
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/4932883.html
Copyright © 2020-2023  润新知