您的位置:首页 > 其它

HDU1074 Doing Homework(状态压缩DP)

2016-05-19 17:27 477 查看

Doing Homework

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

Total Submission(s): 7618    Accepted Submission(s): 3425

[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.
题意:有N个作业,每个作业有结束时间和消耗时间,完成某个作业如果超出限定时间就会扣分。思路:N最大只有15,可以有二进制表示当前作业完成状态,进行dp。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 1<<15
using namespace std;
struct node{
int pre;
int cost;
int cut;
}dp[maxn];
struct course{
char name[110];
int e, need;
}a[20];
bool vis[maxn];
void output(int now){
int last = now^dp[now].pre;
int cnt = 0;
last>>=1;
while(last != 0){
cnt++;
last>>=1;
}
if(dp[now].pre != 0)
output(dp[now].pre);
printf("%s\n", a[cnt].name);
}
int main()
{
int T, i, j, N;
scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
for(int i = 0;i < N;i++) scanf("%s %d %d", a[i].name, &a[i].e, &a[i].need);
memset(vis, 0, sizeof vis);
dp[0].pre = -1;
dp[0].cost = 0;
dp[0].cut = 0;
vis[0] = 1;
for(i = 0;i < ((1<<N)-1);i++){
for(j = 0;j < N;j++){
if((i&(1<<j)) != 0) continue;//已经完成j,跳过
int now = i|(1<<j);
int sum = dp[i].cost + a[j].need;//总时间
int rde = sum - a[j].e;//扣分
if(rde < 0) rde = 0;
rde += dp[i].cut;
if(vis[now]){
if(rde < dp[now].cut){
dp[now].cut = rde;
dp[now].pre = i;
dp[now].cost = sum;
}
}else{
vis[now] = 1;
dp[now].cut = rde;
dp[now].pre = i;
dp[now].cost = sum;
}
}
}
printf("%d\n", dp[(1<<N)-1].cut);
output((1<<N)-1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu dp 状态压缩