hdu1074(dp状态压缩)

14 篇文章 0 订阅
11 篇文章 0 订阅

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分,问怎样安排做作业的顺序才能使得所扣的分最小

#include <iostream>
#include <cstdio>
#include <cstring>
#include<cmath>
using namespace std;
#define maxsize 1<<15
#define inf 1<<30
struct dp
{
    int penalty;   //dp[i].penalty:保存达到状态i下的最小扣分
    int pre;	  //指向对应的前驱
    int now;	 //当前时间
}dp[maxsize];
int vis[maxsize];
struct node 
{
    int deadline;
    int cost;
    char name[101];
};//针对课程
node course[15];
int n;
void print_(int k,int t)
{
    int v,cnt=0;
    v=(k^t);//异或操作找出对应的位置(ex:k=111,t=110则v=001)
    //while(v) { v=v>>1; cnt++; }
	cnt=(log(v*1.0)/log(2.0));
    k=t;
    t=dp[t].pre;
    if(t!=-1)print_(k,t);
    printf("%s\n",course[cnt].name);
}
int main()
{
    int tcase,i,j;
    scanf("%d",&tcase);
    while(tcase--) 
    {
        scanf("%d",&n);
        for(i=0;i<n;i++) scanf("%s %d%d",course[i].name,&course[i].deadline,&course[i].cost);
        int bnumb=1<<n; //二进制表示所有状态种数
        dp[0].now=dp[0].penalty=0;//第0门课程被收入的最小扣分为0;
        dp[0].pre=-1;
        for(i=1;i<=n;i++) dp[i].penalty=inf;
        memset(vis,0,sizeof(vis));
        for(i=0;i<=bnumb-1;i++)
        {//注意是到bnumb-1:2的n次方减一才是他正常的二进制表示形式(n位1)
            for(j=0;j<n;j++)
            {
                int cell,currt,subt,allpenalty;
                int temp=1<<j;
                if((temp&i)==0) //这种情况说明了当前这个j不在我们完成的课程里
                {
                    cell=temp|i;//把这个j加入到了当前状态
                    currt=dp[i].now+course[j].cost;
                    //此课完成时间=当前状态所用时间(dp)+完成此课会消耗的时间
                    subt=currt-course[j].deadline;  //求此时此课扣的分
                    if(subt<0) subt=0;			  
                    allpenalty=dp[i].penalty+subt;
                    if(!vis[cell] || vis[cell] && allpenalty<dp[cell].penalty) 
					{//未访问||访问过了,但扣的分比之前的少
                        dp[cell].penalty=allpenalty;
                        dp[cell].pre=i;
                        dp[cell].now=currt;
						vis[cell]=1;
                    }//对于相应的节点进行修改
                }
            }
        }
        printf("%d\n",dp[(1<<n)-1].penalty);
        int k=(1<<n)-1;
        int t=dp[(1<<n)-1].pre;
        print_(k,t);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值