Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8997 Accepted Submission(s): 4226
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.
2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3
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.
基础状压,二进制数每一位表示01表示作业做和没做,记录一下前驱输出答案即可
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#define maxn (1<<16)+5
using namespace std;
const int inf=0x3f3f3f3f;
int need[maxn],dp[maxn],pre[maxn],pree[maxn];
int c[20],d[20],n;
string ch[20];
int main()
{
int T,i,j;
cin>>T;
while(T--)
{
cin>>n;
memset(need,0,sizeof(need));
memset(dp,inf,sizeof(dp));
dp[0]=0;
for(i=0;i<n;i++)
cin>>ch[i]>>d[i]>>c[i];
for(i=0;i<(1<<n);i++)
{
for(j=0;j<n;j++)
{
if(i&(1<<j))
need[i]+=c[j];
}
}
for(i=0;i<(1<<n);i++)
{
for(j=0;j<n;j++)
{
if((i&(1<<j))==0)
{
if(dp[i|(1<<j)]>(dp[i]+(d[j]<(need[i]+c[j])?need[i]+c[j]-d[j]:0)))
{
pre[i|(1<<j)]=i;
pree[i|(1<<j)]=j;
dp[i|(1<<j)]=dp[i]+(d[j]<(need[i]+c[j])?need[i]+c[j]-d[j]:0);
}
}
}
}
int now=(1<<n)-1,re;
cout<<dp[now]<<endl;
i=0;
string ans[20];
while(now!=0)
{
ans[i++]=ch[pree[now]];
now=pre[now];
}
for(i=n-1;i>=0;i--)
cout<<ans[i]<<endl;
}
}
377




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



