USACO 3.3 Shopping Offers

后边动规没有优化,偷懒了。。。

TASK: shopping
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3060 KB]
   Test 2: TEST OK [0.000 secs, 3060 KB]
   Test 3: TEST OK [0.000 secs, 3060 KB]
   Test 4: TEST OK [0.000 secs, 3060 KB]
   Test 5: TEST OK [0.000 secs, 3060 KB]
   Test 6: TEST OK [0.000 secs, 3060 KB]
   Test 7: TEST OK [0.000 secs, 3060 KB]
   Test 8: TEST OK [0.000 secs, 3060 KB]
   Test 9: TEST OK [0.000 secs, 3060 KB]
   Test 10: TEST OK [0.000 secs, 3060 KB]
   Test 11: TEST OK [0.000 secs, 3060 KB]
   Test 12: TEST OK [0.000 secs, 3060 KB]

All tests OK.

1 /*
2 PROG: shopping
3 ID: jiafeim1
4 LANG: C++
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 int youhui[105][6][2]={0};
11 int youhuicount[105];
12 int convert_youhui[115][6]={0};
13 int youhuijia[105]={0};
14
15 int s;
16 int lookup[1005];
17
18
19 int price[6][6][6][6][6];
20 int target[6];
21 int o_price[6];
22
23
24 int main()
25 {
26 FILE *fin = fopen("shopping.in", "r");
27 FILE *fout = fopen("shopping.out", "w");
28 for(int i = 0;i<1005;++i) lookup[i]=-1;
29
30 fscanf(fin,"%d",&s);
31 int count;
32 for(int i = 0;i<s;++i)
33 {
34
35 fscanf(fin,"%d",&count);
36 (youhuicount[i])=count;
37 for(int j = 0;j<count;++j)
38 {
39 fscanf(fin,"%d%d",&youhui[i][j][0],&youhui[i][j][1]);
40 }
41 fscanf(fin,"%d",&(youhuijia[i]));
42 }
43
44 int b;
45 fscanf(fin,"%d",&b);
46 if(b==0)
47 {
48 fprintf(fout,"0\n");
49 }
50 else
51 {
52
53 for(int i = 0;i<b;++i)
54 {
55 int temp;
56 fscanf(fin,"%d%d%d",&temp,&(target[i]),&(o_price[i]));
57 lookup[temp]=i;
58 }
59
60 int cur=0;
61 for(int i = 0;i<s;++i)
62 {
63 bool flag = false;
64 for(int j =0;j<youhuicount[i];++j)
65 {
66 if(lookup[youhui[i][j][0]]==-1)
67 {
68 flag = true;
69 break;
70 }
71 }
72 if(flag) continue;
73
74 for(int j =0;j<youhuicount[i];++j)
75 {
76 int to = lookup[youhui[i][j][0]];
77 convert_youhui[cur][to]=youhui[i][j][1];
78 }
79 youhuijia[cur]=youhuijia[i];
80 ++cur;
81 }
82 s=cur;
83
84 for(int a0=0;a0<=target[0];++a0)
85 for(int a1=0;a1<=target[1];++a1)
86 for(int a2=0;a2<=target[2];++a2)
87 for(int a3=0;a3<=target[3];++a3)
88 for(int a4=0;a4<=target[4];++a4)
89 {
90 price[a0][a1][a2][a3][a4]
91 =a0*o_price[0]+a1*o_price[1]+a2*o_price[2]+a3*o_price[3]+a4*o_price[4];
92
93 }
94 for(int yh=0;yh<s;++yh)
95 for(int a0=0;a0<=target[0];++a0)
96 for(int a1=0;a1<=target[1];++a1)
97 for(int a2=0;a2<=target[2];++a2)
98 for(int a3=0;a3<=target[3];++a3)
99 for(int a4=0;a4<=target[4];++a4)
100 {
101 if(a0>=convert_youhui[yh][0] &&
102 a1>=convert_youhui[yh][1] &&
103 a2>=convert_youhui[yh][2] &&
104 a3>=convert_youhui[yh][3] &&
105 a4>=convert_youhui[yh][4])
106 {
107 if(price[a0][a1][a2][a3][a4]>
108 youhuijia[yh]+price[a0-convert_youhui[yh][0]]
109 [a1-convert_youhui[yh][1]]
110 [a2-convert_youhui[yh][2]]
111 [a3-convert_youhui[yh][3]]
112 [a4-convert_youhui[yh][4]])
113 {
114 price[a0][a1][a2][a3][a4]=
115 youhuijia[yh]+price[a0-convert_youhui[yh][0]]
116 [a1-convert_youhui[yh][1]]
117 [a2-convert_youhui[yh][2]]
118 [a3-convert_youhui[yh][3]]
119 [a4-convert_youhui[yh][4]];
120 }
121
122 }
123
124 }
125 fprintf(fout,"%d\n",price[target[0]][target[1]][target[2]][target[3]][target[4]]);
126 }
127 fclose(fin);
128 fclose(fout);
129 return 0;
130 }
posted @ 2011-05-28 18:22  幻魇  阅读(371)  评论(0编辑  收藏  举报