zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    JS PopupAlert
    JS Navigator
    JS History
    JS Location
    JS Screen
    JS Window
    PTZView
    使用JmDNS发现设备
    Android开发调用webservice方式之一
    解决webservice发布报错Service Unavailable HTTP Error 503. The service is unavailable.
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/4932883.html
Copyright © 2011-2022 走看看