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.
这个题真是浪费我好久好久…
在dp入门的专题里做到这个第一波我都不明白这是个什么东西
直到第二波回头看的时候才懂了…
所谓状态压缩dp怎么说呢我觉得特征有那么几个
第一个就是不确定的维度
因为维度的不确定所以用了二进制来表示…
第二个就是数据量一般都不大…
他没法大
空间复杂度2^n这个分分钟爆炸
知道他是状压dp了以后剩下的好办不少…
从1一直到2^n这么多种状态,每个状态都是所有种物品的不同形态
这时候要看仔细了,每一个物品的状态变化到底都和谁有关
和谁有关就转移缺了谁的状态….
这个状态怎么找…还是用1<
#include<iostream>
#include<memory.h>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;
struct p
{
string n;
int jieshu, hua;
};
struct q
{
int qian, xian, time, fen;
};
p zhi[17];
q dp[1<<15];
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
for (int a = 0;a < n;a++)cin >> zhi[a].n >> zhi[a].jieshu >> zhi[a].hua;
int quanbu = 1 << n ;
memset(dp, 0, sizeof(dp));
for (int qingk = 1;qingk < quanbu;qingk++)
{
dp[qingk].fen = 1<<30;
for (int a = n - 1;a >= 0;a--)
{
int t = 1 << a;
if (t&qingk)
{
int qian = qingk - t;
int benci = zhi[a].hua + dp[qian].time - zhi[a].jieshu;
if (benci < 0)benci = 0;
if (benci + dp[qian].fen < dp[qingk].fen)
{
dp[qingk].fen = benci + dp[qian].fen;
dp[qingk].xian = a;
dp[qingk].qian = qian;
dp[qingk].time = dp[qian].time + zhi[a].hua;
}
}
}
}
stack<int>ww;
int tem = quanbu - 1;
while (tem)
{
ww.push(dp[tem].xian);
tem = dp[tem].qian;
}
cout << dp[quanbu-1].fen << endl;
while (!ww.empty())
{
cout << zhi[ww.top()].n << endl;
ww.pop();
}
}
return 0;
}

本文深入解析了一道关于状态压缩动态规划的问题,通过一个具体的例子详细介绍了如何利用二进制表示不同状态,以及如何进行状态转移以求解最小化分数损失的问题。
857

被折叠的 条评论
为什么被折叠?



