Doing Homework HDU - 1074 (状压dp)

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

题意:你有几门课的作业要完成,有个完成作业的时限和要完成作业所花的时间,完成作业的时间每超出时限1天就扣1分

现在让你选择按什么顺序完成作业扣分最少

解 :花费时间是固定的只有完成的顺序会有影响 ,我们把所有完成的情况压缩成1个点

比如 n=2

0 0  没有作业完成

0 1  只有第二项作业完成

1 0  只有第二项作业完成

1 1  作业全完成

从 0 1 和 1 0 都可以到达 1 1 我们记录的是扣分少的

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
int dp[1<<15];//每1个状态扣分的最小值
int pre[1<<15];//记录路径
int t,n;
struct node
{
    int x,y;
    string s;
}e[20];
void output(int x)//输出路径
{
    if(x==0) return;
    int t=pre[x]^x;//路径上的两个值只有1个位置不一样
    int i;
    for(i=0;i<n;i++)
    {
        if((1<<i)==t) //找到不一样的位置
            break;
    }
    output(pre[x]);//继续往前找
   cout<<e[i].s<<endl;
}
int main()
{
 scanf("%d",&t);
 while(t--)
 {
     scanf("%d",&n);
     memset(dp,inf,sizeof(dp));
     memset(pre,0,sizeof(pre));
    for(int i=0;i<n;i++)
        cin>>e[i].s>>e[i].x>>e[i].y;
     dp[0]=0;
     for(int i=0;i<(1<<n);i++)//枚举每个状态
     {
         for(int j=0;j<n;j++)//枚举寻找没被完成的作业
         {
             if(i&(1<<j)) continue;//如果当前作业已经被完成
             int s=0;//记录这个状态所花的时间
             for(int k=0;k<n;k++)
             {
                 if(i&(1<<k))//如果这个作业已经被完成,所花时间增加
                s+=e[k].y;
             }
            s+=e[j].y;//增加要完成枚举的这个作业的时间
            if(s>e[j].x) s=s-e[j].x; //如果超过时限,s记为完成该作业扣的分
            else s=0;
            if(dp[i|(1<<j)]>dp[i]+s)//如果从这个状态到下个状态扣得分更少
            {
                dp[i|(1<<j)]=dp[i]+s; //更新
                pre[i|(1<<j)]=i;//记下路径
            }
         }
     }
     printf("%d\n",dp[(1<<n)-1]);
     output((1<<n)-1);
 }
}


猜你喜欢

转载自blog.csdn.net/dsaghjkye/article/details/80256244