Background of topic
Adventure to Tianlu Junior A Part 3
Title Description
With the help of Monkey King, little A finally walked out of this barren hill, but found a rough River in front of him. There are no boats on the river, but fortunately there are n diving tools in Xiao A. Because he had to carry heavy backpacks, he could only carry m-weighted tools, and because his strength was not unlimited, the river was wide, so he could only carry tools with v resistance. But there are very important data under the river, so he wants to stay for the longest time. So he found you and asked you to tell him the plan.
Input format
Three numbers m,v,n, as the title says
Next n rows, three numbers ai, Bi and CI in each row represent the gravity, resistance, and time to support.
Output format
A number in the first line, representing the longest time
The next line, several numbers, represents the selected item
Input and Output Samples
Input #1 replication
100 100 3
50 60 289
40 10 116
50 50 106
Output #1 replication
405
1 2
Note/hint
1<=m,v<=200,n<=100
Data assurance must have a plan.
If there are many schemes, output the smallest one before.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int maxn = 205; int mm[maxn],vv[maxn],tt[maxn]; struct DP { int id[100]; int l; int t; }dp[maxn][maxn]; int main() { int m,v,n;scanf("%d%d%d",&m,&v,&n); for(int i = 1;i <= n;i++) { scanf("%d%d%d",&mm[i],&vv[i],&tt[i]); } for(int i = 1;i <= n;i++) { for(int j = m;j >= mm[i];j--) { for(int k = v;k >= vv[i];k--) { if(dp[j][k].t < dp[j - mm[i]][k - vv[i]].t + tt[i]) { dp[j][k].t = dp[j - mm[i]][k - vv[i]].t + tt[i]; int l; for(l = 1;l <= dp[j - mm[i]][k - vv[i]].l;l++) { dp[j][k].id[l] = dp[j - mm[i]][k - vv[i]].id[l]; } dp[j][k].l = dp[j - mm[i]][k - vv[i]].l + 1; dp[j][k].id[dp[j][k].l] = i; } } } } printf("%d\n",dp[m][v].t); for(int i = 1;i <= dp[m][v].l;i++) { printf("%d ",dp[m][v].id[i]); } return 0; }