【HDU-1074】Doing Homework (动态规划 状压DP)

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
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.

题意

给定 n 个作业 “作业名字 作业截止时间 作业消耗时间”。交作业每晚一天扣 1 分,求扣分最小的做作业安排。

题解

首先观察一下 N 的大小是小于 15 的,所以可以猜想是状压dp。
我们首先思考当前状态可以由那些状态转移哪些状态。
我们用 d p [ 01 ] dp[01串的十进制] 表示 01 串中的 1 1 的位数的作业已经完成所扣的最小分数。
例如 d p [ 01101 ] dp[01101] 表示完成了第 1 3 4 1,3,4 个作业的最小扣分。
然而 01101 01101 可以转移到 11101 11101 或者 01111 01111 。即 d p [ 01101 ] &gt; d p [ 11101 ] dp[01101] -&gt; dp[11101] 或者 d p [ 01101 ] &gt; d p [ 01111 ] dp[01101] -&gt; dp[01111]
最后 d p [ 11111 ] dp[11111] 就是完成 五个作业的最小扣分。
那这个扣分怎么算,我们用 t [ 01 ] t[01串] 表示完成了这个 01串的作业所需要的时间。
扣分就是 time[i] + a[j].cost - a[j].deadline;
因此我们可以状态转移为:

int total = 1 << n;
for (int i = 0; i < total; i++) { //枚举所有01串
      for (int j = 0; j < n; j++) { //枚举下一个完成的作业
        if ((i&(1 << j)) == 0) { // 如果这个作业还没有完成
          int t = time[i] + a[j].cost - a[j].deadline; // 在当前状态下完成改作业要扣的分数
          t = t <= 0? 0:t; // 如果不用扣分,则为 0
          if (dp[i|(1 << j)] > t + dp[i]) {
          	//转移
          }
        }
      }
    }

接下来的剩下的问题就是如何去记录路径。我们用 p r e [ 01 ] pre[01串] 来保存这个转移到01串所做的作业编号。

int total = (1 << n);
dp[0] = 0;
for (int i = 0; i < total; i++) {
  for (int j = 0; j < n; j++) {
    if ((i&(1 << j)) == 0) {
      int t = time[i] + a[j].cost - a[j].deadline;
      t = t <= 0? 0:t;
      if (dp[i|(1 << j)] > t + dp[i]) {
        dp[i|(1 << j)] = dp[i] + t;
        time[i|(1 << j)] = time[i] + a[j].cost;
        pre[i|(1 << j)] = i;
        now[i|(1 << j)] = j;
      }
    }
  }
}

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <cstring>
using namespace std;
const int maxn = 1 << 16;
int T, n, dp[maxn], time[maxn], pre[maxn], now[maxn];
struct node {
  char name[110];
  int cost, deadline;
} a[20];

int main()
{
  scanf("%d", &T);
  while (T--) {
    memset(time, 0, sizeof time);
    memset(pre, -1, sizeof pre);
    fill(dp, dp + maxn, 0x3fffffff);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%s %d %d", a[i].name, &a[i].deadline, &a[i].cost);
    }
    int total = (1 << n);
    dp[0] = 0;
    for (int i = 0; i < total; i++) {
      for (int j = 0; j < n; j++) {
        if ((i&(1 << j)) == 0) {
          int t = time[i] + a[j].cost - a[j].deadline;
          t = t <= 0? 0:t;
          if (dp[i|(1 << j)] > t + dp[i]) {
            dp[i|(1 << j)] = dp[i] + t;
            time[i|(1 << j)] = time[i] + a[j].cost;
            pre[i|(1 << j)] = i;
            now[i|(1 << j)] = j;
          }
        }
      }
    }

    printf("%d\n", dp[total - 1]);
    char ans[18][110];
    int cnt = 0;
    for (int i = total - 1; i != 0; i = pre[i]) {
      strcpy(ans[cnt++], a[now[i]].name);
    }
    for (int i = cnt - 1; i >= 0; i--) {
      puts(ans[i]);
    }
  }

  return 0;
}
发布了79 篇原创文章 · 获赞 56 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qq_40861916/article/details/84259234