您的位置:首页 > 其它

hdu 1074 Doing Homework

2012-10-16 23:22 323 查看
题目描述:

Doing Homework

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3279 Accepted Submission(s): 1272



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3


[align=left]Sample Output[/align]

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.


[align=left]Author[/align]
Ignatius.L
我的思路:

本来以为是搜索题,小写了一个居然TLE,没办法,只能另辟蹊径,然后想到状态压缩dp。

分析题意就是需要重排这十五个人的位置,然后求得最优解。

十五个人是否已排可用0,1表示,于是就变成二进制数,比如3的二进制及时(101),表示1,3已排,2未排。

目标状态是dp[(1<<n)-1];对于某个状态m,枚举人i,判断此人在此状态中是否属于已排的。相当于判断(m&(1<<i))是否为一,是的话就可以写转移方程:

dp[m]=max(dp[v]+w(v,i)) 其中(0<=i<n,v=m-(1<<i));

用记忆话搜索就搞定。

对了,本题需要字典序最小,其实在搜索前先排下序就可以了。附上AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<cstdlib>

#define BUG puts("ok");
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define inf 16843009

using namespace std;
int const nMax=1000;
typedef long long LL;

struct Int
{
char s[110];
int d,c;
bool operator < (const Int &b)const{
return strcmp(s,b.s)<0;
}
};

Int a[20];
int n;
int dp[nMax],t[nMax],fa[nMax];

int DP(int u){
if(u==0)return dp[u]=0,t[u]=0;
if(dp[u]!=inf)return dp[u];
int v,k;
for(int i=0;i<n;i++){
if(u&(1<<i)){
v=u-(1<<i);
k=DP(v);
t[u]=t[v]+a[i].c;
if(t[u]>a[i].d){
if(dp[u]>=k+t[u]-a[i].d){//注意等号,因为要字典序。。。。。
dp[u]=k+t[u]-a[i].d;
fa[u]=i;
}
}else {
if(dp[u]>=k){
dp[u]=k;
fa[u]=i;
}
}
}
}
return dp[u];
}

void output(int m){
if(m==0)return ;
output(m-(1<<fa[m]));
printf("%s\n",a[fa[m]].s);
return ;
}

int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s%d%d",a[i].s,&a[i].d,&a[i].c);
}
sort(a,a+n);
memset(dp,1,sizeof(dp));//赋值为无穷大
int ans=DP((1<<n)-1);
printf("%d\n",ans);
int m=((1<<n)-1);
output(m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: