137人阅读 评论(0)

# Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7660    Accepted Submission(s): 3454

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

#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace std;
const int maxn=1e9;
int dp[1<<16][20];
int bp[1<<16][20];
pair<int,int> pre[1<<16][20];
int n;
struct Node
{
string s;
int st;
int time;
}a[20];
void dfs(int s,int pos)
{
if(s==-1&&pos==-1)
return;
dfs(pre[s][pos].first,pre[s][pos].second);
cout<<a[pos].s<<endl;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
cin>>a[i].s>>a[i].st>>a[i].time;
int s=(1<<n)-1;
for(int i=0;i<=s;i++)
for(int j=1;j<=n;j++)
dp[i][j]=maxn;
for(int i=1;i<=n;i++)
{
dp[1<<(i-1)][i]=max(0,a[i].time-a[i].st);
bp[1<<(i-1)][i]=a[i].time;
pre[1<<(i-1)][i]=make_pair(-1,-1);
}
for(int i=1;i<=s;i++)
{
for(int j=n;j>=1;j--)
{
if(!(i&(1<<(j-1))))
continue;
for(int k=1;k<=n;k++)
{
if(i&(1<<(k-1)))
continue;
int state=i+(1<<(k-1));
if(dp[state][k]>dp[i][j]+max(bp[i][j]+a[k].time-a[k].st,0))
{
dp[state][k]=dp[i][j]+max(bp[i][j]+a[k].time-a[k].st,0);
bp[state][k]=bp[i][j]+a[k].time;
pre[state][k]=make_pair(i,j);
}
//else if(dp[state][k]==dp[i][j]+max(bp[i][j]+a[k].time-a[k].st,0)&&a[pre[state][k].second].s>a[j].s)
//{
// pre[state][k]=make_pair(i,j);
//}
}
}
}
int ans=maxn;
int pos=0;
for(int i=n;i>=1;i--)
{
if(ans>dp[s][i])
{
ans=dp[s][i];
pos=i;
}
}
printf("%d\n",ans);
dfs(s,pos);

}
return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：74840次
• 积分：4009
• 等级：
• 排名：第7778名
• 原创：341篇
• 转载：0篇
• 译文：0篇
• 评论：7条
阅读排行
最新评论