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\n", 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\n", 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;
}

转载于:https://www.cnblogs.com/immortal-worm/p/4932883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值