您的位置:首页 > 其它

HDU1074——Doing Homework(状态压缩DP)

2014-08-20 23:08 330 查看


Doing Homework

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5238 Accepted Submission(s): 2190



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门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。

这n门作业按课程的字典序先后输入

问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序

PS:达到扣分最少的方案有多种,请输出字典序最小的那一组方案

分析:

给出的n<=15,所以可以枚举所有的状态。

用一个1<<n内的整数可以表示一个状态,例如n=3的所有状态就是 001,010,011,100,101,110,111,每一位的0和1,对应某项作业做没做。

这样可以枚举出所有的状态,剩下的就是状态之间的转移。

对于每个状态,都可以尝试完成还没有完成的作业,即把某位的0变成1. 如:001 -->101 ,001-->011

而最终还要输出作业的顺序,所以要开一个数组,在每次成功更新某个状态的同时更新路径的前驱。

然后将路径递归输出。

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#define INF 0x7fffffff
using namespace std;

struct node
{
    char s[200];
    int d,c;
}w[20];
int dp[1<<16],time[1<<16],pre[1<<16];

void init()
{
    for(int i=0;i< 1<<16 ;i++)
    {
        dp[i]=INF, time[i]=0, pre[i]=-1;
    }
}
void output(int x)
{
    if(x==0) return;
    
    output(x-(1<<pre[x]));
    printf("%s\n",w[pre[x]].s);
}
int main()
{
    int T,n,cost;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s%d%d",w[i].s,&w[i].d,&w[i].c);

        int gal=(1<<n)-1;
        dp[0]=0;
        for(int i=0;i<gal;i++)
        {
            for(int j=0;j<n;j++)          //尝试做未完成的作业
            {
                int k=1<<j;
                if((i&k)!=0) continue;    //该位置的作业已经做了

                time[i|k]=time[i]+w[j].c; //累加时间。
                if(time[i|k]>w[j].d)      //判断是否超过期限。
                    cost=time[i|k]-w[j].d;
                else
                    cost=0;

                if(dp[i|k]>dp[i]+cost)  //更新最少扣分,和路径前驱。
                    dp[i|k]=dp[i]+cost, pre[i|k]=j;
            }
        }

        printf("%d\n",dp[gal]);
        output(gal);
    }

	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: