hdu1074 参考答案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include
25 #include
26 using namespace std;
27 const int N=15;
28 const int M=65536;
29 char s[N][110];
30 int d[N],c[N];
31 int n;
32 struct Status
33 {
34 int reduce;
35 int time;
36 int last;
37 };
38 Status ans[M];
39 void Init()
40 {
41 scanf("%d",&n);
42 for(int i=0;i<n;i++)
43 scanf("%s %d %d",s[i],&d[i],&c[i]);
44 memset(ans,-1,sizeof(ans));
45 }
46 void DP()
47 {
48 ans[0].time=0;
49 ans[0].reduce=0;
50 int max=(1<<n)-1;
51 int next;
52 for(int k=0;k<max;k++)
53 {
54 if(ans[k].reduce==-1)
55 continue;
56 for(int i=0;i<n;i++)
57 {
58 int tmp=(k>>i);
59 if((tmp&1)==0)
60 {
61 next=k+(1<<i);
62 }
63 else
64 continue;
65 ans[next].time=ans[k].time+c[i];
66 tmp=ans[k].reduce;
67 if(ans[next].time>d[i])
68 tmp+=ans[next].time-d[i];
69 if(ans[next].reduce==-1||ans[next].reduce>tmp)
70 {
71 ans[next].reduce=tmp;
72 ans[next].last=i;
73 }
74 }
75 ans[k].reduce=-1;
76 }
77 }
78 void Output()
79 {
80 int t;
81 t=(1<<n)-1;
82 printf("%d\n",ans[t].reduce);
83 int out[N];
84 int k=0;
85 while(ans[t].last>=0)
86 {
87 out[k++]=ans[t].last;
88 t=t-(1<<ans[t].last);
89 }
90 for(int k=n-1;k>=0;k--)
91 printf("%s\n",s[out[k]]);
92 }
93 int main()
94 {
95 int test;
96 scanf("%d",&test);
97 while(test-->0)
98 {
99 Init();
100 DP();
101 Output();
102 }
103 }
hdu1074 参考答案 (2008-11-12 14:22:36由218编辑)