您的位置:首页 > 其它

hdu 1074 doing homework(状态压缩dp)

2016-02-27 15:28 483 查看
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.


状态压缩DP,我们可以使用一个二进制的数来表示做作业的状态,1表示做了,0表示没做

dp[i]表示状态i损失的分数,再做一个作业x可到另一状态dp[j],则要有i&x==0,若要有dp[a],dp[b]均可以到达dp[j],那么选择损失分数最小的,若分数相同,按题意的字典序,即选择a和b中较小的一个,因为题目的课程是按字典序给出的。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node
{
int time, pre, cost;
}dp[1<<15];
struct Node
{
int deadline, work;
char subject[200];
}a[15];
void fun(int t)
{
int i, num=0;
i = dp[t].pre^t;
i >>= 1;
while (i)
{
num++;
i >>= 1;
}
if (dp[t].pre)
fun(dp[t].pre);
printf("%s\n", a[num].subject);
}
int main()
{
int t, n, i, flag[1 << 15], j, k, cur, temp;
cin >> t;
while (t--)
{
cin >> n;
for (i = 0;i < n;i++)
cin >> a[i].subject >> a[i].deadline >> a[i].work;
int t1 = (1 << n) - 1;
memset(flag, 0, sizeof(flag));
dp[0].time = 0, dp[0].pre = -1, dp[0].cost = 0, flag[0] = 1;
for (i = 0;i < t1;i++)
{
for (j = 0;j < n;j++)
{
cur = 1 << j;
if ((cur&i) == 0)
{
k = cur | i;
dp[k].time = dp[i].time + a[j].work;
temp = dp[k].time - a[j].deadline;
if (temp < 0) temp = 0;
temp += dp[i].cost;
if (flag[k])
{
if (temp < dp[k].cost)
{
dp[k].cost = temp;
dp[k].pre = i;
}
else if (temp == dp[k].cost)
{
if (dp[k].pre>i)
dp[k].pre = i;
}
}
else
{
flag[k] = 1, dp[k].cost = temp, dp[k].pre = i;
}
}
}
}
cout << dp[t1].cost << endl;
fun(t1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: