-
Notifications
You must be signed in to change notification settings - Fork 0
/
backpack.c
112 lines (92 loc) · 2.37 KB
/
backpack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<stdio.h>
int max(int a,int b)
{
if(a>b) return a;
else return b;
}
int main()
{
int t,vm,n;
int v[62],c[62],u[62];
int va[188],ca[188],flag[188];
int mtg[62];
int dp[62][3202];
scanf("%d",&t);
while(t)
{
scanf("%d %d",&vm,&n);
vm=vm/10;
int i,j;
for(i=1;i<=n;i++)
{
scanf("%d %d %d",&v[i],&c[i],&u[i]);
v[i]=v[i]/10;
}
for(i=0;i<=vm;i++)
dp[0][i]=0;
for(i=1;i<=n;i++)
dp[i][0]=0;
for(i=1;i<=n;i++)
{
if(u[i]!=0)
{
k=3*(u[i]);
if(flag[k]!=1)
{
va[k]=v[i];
ca[k]=c[i];
k++;
flag[k]=1;
}
}
}
for(i=1;i<=n;i++)
{
if(u[i]==0)
{
for(j=1;j<=vm;j++)
{
if(j>=v[i])
{
if (dp[i-1][j]<=v[i]*c[i]+dp[i-1][j-v[i]])
{
dp[i][j]=v[i]*c[i]+dp[i-1][j-v[i]];
}
else
{
dp[i][j]=dp[i-1][j];
//mtg[i]=0;
}
}
else
dp[i][j]=dp[i-1][j];
}
}
else
{
if(mtg[u[i]]==1)
{
for(j=1;j<=vm;j++)
{
if(j>=v[i]+v[u[i]])
{
if (dp[i-1][j]<=v[i]*c[i]+dp[i-1][j-v[i]])
{
dp[i][j]=v[i]*c[i]+dp[i-1][j-v[i]];
mtg[i]=1;
v[u[i]]=v[u[i]]+v[i];
//iprev=i;
}
else
dp[i][j]=dp[i-1][j];
}
else
dp[i][j]=dp[i-1][j];
}
}
}
}
printf("%d\n",dp[n][vm]*10);
t--;
}
}