Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10900 Accepted Submission(s): 5268
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.
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<bits/stdc++.h>
using namespace std;
int pre[1<<15+2];
struct cals
{
char t[102];
int time,cost;
}c[16];
struct node
{
int id,t,c;
}dp[1<<15+2];
stack<int>P;
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n;scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s%d%d",&c[i].t,&c[i].time,&c[i].cost);
memset(dp,0,sizeof(dp));
pre[0]=-1;
for(int s=1;s<(1<<n);s++)
{
dp[s].c=1e9;
for(int i=n-1;i>=0;i--)//因为要求多解的时候输出字典序
//最小的一组,因此这里要从n-1到0遍历。
{
if(s>>i&1)
{
int r=s-(1<<i);
int tt=dp[r].t+c[i].cost-c[i].time;
if(tt<0)tt=0;
if(dp[r].c+tt<dp[s].c)
{
dp[s].c=dp[r].c+tt;
pre[s]=r;
dp[s].t=dp[r].t+c[i].cost;
dp[s].id=i;
}
}
}
}
int tmp=(1<<n)-1;
printf("%d\n",dp[tmp].c);
while(pre[tmp]!=-1)
{
P.push(dp[tmp].id);
tmp=pre[tmp];
}
while(!P.empty())
{
printf("%s\n",c[P.top()].t);
P.pop();
}
}
return 0;
}
作业调度算法优化
本文介绍了一个基于时间限制和资源约束的作业调度问题,通过合理的安排作业顺序来最小化因逾期而减少的成绩分数。使用了动态规划的方法求解,并提供了一个完整的C++实现代码。
3万+

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



