Total Submission(s): 3778 Accepted Submission(s): 1511
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.
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 MathHintIn 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.
Author
Ignatius.L
感觉不错。。。
状态压缩dp适用于状态较多但是数据量很小的情况下。。。。
对于给定的n个作业那么用二进制表示1为做了0为没做那么就可以表示1<<n个状态了。。。
每个状态保存到达该状态的时间花费以及上一个状态等。。。
还有就是字典序的问题题目给的条件很好需要仔细读题。。。
还有一个注意点就是name的副本保存:
strcpy(Name[1<<i],c[i].name);
这由打印的代码决定:
void printf_ans(int in)
{
int cur = in ^ dp[in].pre;//相邻两个状态可以通过异或得到具体是哪位二进制改变了
if(dp[in].pre != -1)
{
printf_ans(dp[in].pre);
printf("%s\n",Name[cur]);
//printf("%s\n",Name[in ^ dp[in].pre]);
}
}
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <stdlib.h>
#include<cctype>
#define rep(i,s,e) for(int i = (s);i<=(e);++i)
#define maxn 1<<16
#define INF 1000000000
using namespace std;
struct course
{
char name[200];
int dead;
int day;
}c[16];
struct state
{
int cost;//该状态下的最小扣分
int time;//到达该状态时的时间
int pre;//前一个状态用来递归打印
}dp[maxn];
char Name[maxn][200];
int n;
int vis[maxn];
int ans[20];
void printf_ans(int in)
{
int cur = in ^ dp[in].pre;//相邻两个状态可以通过异或得到具体是哪位二进制改变了
if(dp[in].pre != -1)
{
printf_ans(dp[in].pre);
printf("%s\n",Name[cur]);
//printf("%s\n",Name[in ^ dp[in].pre]);
}
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
int next;
int cut;//扣分
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 0;i<n;++i)
{
scanf("%s%d%d",c[i].name,&c[i].dead,&c[i].day);
strcpy(Name[1<<i],c[i].name);
}
int max_state = (1<<n)-1;//最大的状态作业全部都做了
//初始化啥都没做的时候
memset(vis,0,sizeof(vis));
dp[0].time = 0;
dp[0].cost = 0;
dp[0].pre = -1;
vis[0] = 1;
int temp;
for(int i = 0;i<=max_state;++i)//枚举所有的状态
for(int j = 0;j<n;++j)//对应状态下分别尝试去做所有的作业
{
temp = (1<<j);//尝试做第j个作业
if((i&temp)==0)//该课还未做
{
next = temp|i;//下一个状态也就是把该课做了i为当前状态
dp[next].time = dp[i].time + c[j].day;
cut = max(0,dp[next].time - c[j].dead);
cut += dp[i].cost;
if(!vis[next])
{
vis[next] = 1;
dp[next].cost = cut;
dp[next].pre = i;
}
else
{
if(dp[next].cost > cut)
{
dp[next].cost = cut;
dp[next].pre = i;
}else
{
if(dp[next].cost == cut)
{
if(dp[next].pre > i)//注意题目给的要求。。题目是按照字典序升序给的数据
{
dp[next].pre = i;
}
}
}
}
}
}
printf("%d\n",dp[max_state].cost);
int m = max_state;
printf_ans(m);
}
return 0;
}