hdu1074 状态压缩dp

 1 #include<stdio.h>
 2 #include<string.h>
 3 char s[20][105];
 4 int ded[20],day[20],hash[40000];
 5 struct s
 6 {
 7     int time,pre,lost;
 8 }dp[40000];
 9 void  Print(int state)
10 {
11     if  (state==0) return;
12     Print(state^(1<<dp[state].pre));
13     printf("%s\n",s[dp[state].pre]);
14 }
15 int main()
16 {
17     int T,n,i,j,cur,state,temptime,templost;
18     scanf("%d",&T);
19     while (T--)
20     {
21         scanf("%d",&n);
22         getchar();
23         for (i=0;i<n;i++) scanf("%s %d %d",s[i],&ded[i],&day[i]);
24         state=(1<<n);
25         memset(hash,0,sizeof(hash));
26         dp[0].time=0; dp[0].pre=-1; dp[0].lost=0;
27         for (i=0;i<state;i++)
28          for (j=0;j<n;j++)
29          if ((i&(1<<j))==0)
30          {
31              cur=(i|(1<<j));
32              temptime=dp[i].time+day[j];
33              if (temptime<=ded[j]) templost=dp[i].lost;
34              else templost=dp[i].lost+temptime-ded[j];
35              if (hash[cur]==0)
36              {
37                  dp[cur].time=temptime;
38                  dp[cur].pre=j;
39                  dp[cur].lost=templost;
40                  hash[cur]=1;
41              }
42              else if (dp[cur].lost>templost)
43              {
44                  dp[cur].time=temptime;
45                  dp[cur].pre=j;
46                  dp[cur].lost=templost;
47              }
48          }
49         printf("%d\n",dp[state-1].lost);
50         Print(state-1);
51     }
52 }

http://acm.hdu.edu.cn/showproblem.php?pid=1074

posted on 2014-07-22 14:17  xiao_xin  阅读(99)  评论(0编辑  收藏  举报

导航