HDU 1074 Doing Homework 状态压缩dp

Doing Homework

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


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

 
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次。
 
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<queue>

 6 using namespace std;

 7 

 8 struct node

 9 {

10     char a[102];

11     int d_line;

12     int f_time;

13 }f[16];

14 struct st

15 {

16     int cost;

17     int stime;

18     int state;

19     int i;

20     char a[17];

21 } dp[1<<15];

22 queue<st>Q;

23 void solve(int n)

24 {

25     st n1,n2;

26     int i;

27     n1.cost=n1.stime=0;

28     n1.a[0]='\0';

29     n1.state=0;

30     n1.i=-1;

31     dp[0]=n1;

32     Q.push(n1);

33     while(!Q.empty())

34     {

35         n1=Q.front();

36         Q.pop();

37         for(i=0; i<n; i++)

38         {

39             if( (n1.state&(1<<i)) == 0 )

40             {

41                 n2=n1;

42                 n2.state = (n1.state | (1<<i));

43                 n2.stime = n1.stime+f[i].f_time;

44                 if(n2.stime>f[i].d_line)

45                     n2.cost=n2.cost+(n2.stime-f[i].d_line);

46                 if( dp[n2.state].cost==-1 || n2.cost<dp[n2.state].cost )

47                 {

48                     n2.i=n1.i+1;

49                     n2.a[n2.i]=i+'0';

50                     n2.a[n2.i+1]='\0';

51                     dp[n2.state]=n2;

52                     Q.push(dp[n2.state]);

53                 }

54             }

55         }

56     }

57     printf("%d\n",dp[(1<<n)-1].cost);

58     for(i=0;i<n;i++)

59         printf("%s\n",f[dp[(1<<n)-1].a[i]-'0'].a );

60 }

61 int main()

62 {

63     int T,n,i,k;

64     scanf("%d",&T);

65     while(T--)

66     {

67         scanf("%d",&n);

68         for(i=0; i<n; i++)

69             scanf("%s%d%d",f[i].a,&f[i].d_line,&f[i].f_time);

70         while(!Q.empty())   Q.pop();

71         for(i=0,k=1<<n; i<k; i++)

72         {

73             dp[i].cost=-1;

74             dp[i].a[0]='\0';

75             dp[i].state=0;

76             dp[i].stime=0;

77             dp[i].i=0;

78         }

79         solve(n);

80     }

81     return 0;

82 }

 

你可能感兴趣的:(home)