Doing Homework HDU - 1074

来自:https://blog.csdn.net/lpeaceminusone/article/details/81383182
添加了注释

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最大是15,所以可以把每个作业表示为0或1两种状态,然后dp 找出最少扣分的顺序。
状压dp的本质:一方面是有or没有用1,0记录,二进制思想 ,一方面是二进制对应的数
思路:不是选了一门课后看所有惩罚,是选最优路径,当选到一个,再看对该课应该罚多少

#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define maxn (1<<15)+10
#define inf 0x3f3f3f3f
#define ll long long
#define mod 7
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
struct node{
    
    
    char s[105];
    int a,b;
}d[maxn];
int dp[maxn],pre[maxn],time[maxn];
void print(int x){
    
    
    if(x==0)return ; 
    print(x-(1<<pre[x]));//上一次情况 
    cout<<d[pre[x]].s<<endl;
}//pre记录位数,是从上一次到来的科目序号 
//状压dp的本质:一方面是有or没有用1,0记录,二进制思想 ,一方面是二进制对应的数 
int main(){
    
    
	FILE*fp=fopen("text.in","r");
    int t;
    fscanf(fp,"%d",&t);
    while(t--){
    
    
     mem(time,0);mem(pre,0);mem(dp,0);
     int n;
     fscanf(fp,"%d",&n);//n:总个数 
     for(int i=0;i<n;i++){
    
    
        fscanf(fp,"%s%d%d",d[i].s,&d[i].a,&d[i].b);//s:名字 a:期限 b:天数 
     }//存数据于d[i] 
     int total=1<<n;//total-1:全完成 
     for(int i=1;i<total;i++){
    
    //i:各状态 
        dp[i]=inf;//无穷大 
        for(int j=n-1;j>=0;j--){
    
    //用位数循环 
            int temp=1<<j; //一门课 
            if(!(i&temp))continue; //该门课应该上,否则下一个 
            int s=time[i-temp]+d[j].b-d[j].a;//之前用时加现在用时减最后期限 
            s=s<0?0:s;//s大于0,超时了,罚 s天的分数 。注意赋值给s!! 
            if(dp[i]>dp[i-temp]+s){
    
    //i:做了,i-temp:没做 ,开始无穷大,若可以缩小时间就缩小 
                dp[i]=dp[i-temp]+s;//减少罚时 
                time[i]=time[i-temp]+d[j].b;//i状态用时,如果变长了都会在选之后的课的s体现,所以始终最优 
                pre[i]=j;//上一次写了1<<j,记录路径 
            }//思路:不是选了一门课后看所有惩罚,是选最优路径,当选到一个,再看对该课应该罚多少 
        }
     }
     printf("%d\n",dp[total-1]);
     print(total-1);
    }
}

我写的,喜欢放一起:
有四处细节错误!!

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 16
#define maxn ((1<<15)+10) 
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
struct node{
    
    
	char s[105];
	int d,c;
}lib[N];
struct dp{
    
    
	int pre,time,dp1;
}dp[maxn];
void print(int x){
    
    
	if(x==0)return;
	print(x-(1<<dp[x].pre));
	printf("%s\n",lib[dp[x].pre].s);
}

int main()
{
    
    
	//FILE*fp=fopen("text.in","r");------记得最后注释掉!! 
	int t;
	//fscanf(fp,"%d",&t);
	scanf("%d",&t);
	while(t--){
    
    
		int n;
		//fscanf(fp,"%d",&n);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
    
    
			//fscanf(fp,"%s%d%d",lib[i].s,&lib[i].d,&lib[i].c);
			scanf("%s%d%d",lib[i].s,&lib[i].d,&lib[i].c);
		}
		mem(dp,0);
		int total=1<<n;
		for(int i=1;i<total;i++){
    
    
			dp[i].dp1=inf;
			for(int j=n-1;j>=0;j--){
    
    
				int temp=1<<j;
				if(!(temp&i))continue;
				int s=dp[i-temp].time+lib[j].c-lib[j].d;
				s=s>0?s:0;//s=!!
				if(dp[i].dp1>s+dp[i-temp].dp1){
    
    
					dp[i].dp1=s+dp[i-temp].dp1;
					dp[i].time=dp[i-temp].time+lib[j].c;//i-temp!!
					dp[i].pre=j;
				}
			}
		}
		printf("%d\n",dp[total-1].dp1);//dp1!!
		print(total-1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51945248/article/details/113868874