Fate
problem DescriptionRecently Xhd is playing a game called fate, in order to get the best equipment, XHD in the constant killing blame do tasks. Over time XHD began to hate the killing monsters, but also had to kill the blame to rise to the last level. Now the problem is, XHD up the last level also need n experience value, XHD still have M endurance, each kill a strange xhd will get corresponding experience, and reduce the corresponding endurance. When endurance drops below 0 or 0, XHD will not play the game.
XHD also said that he only killed s only strange. Can he raise this last level, please?
InputThe input data has multiple groups, and for each set of data the first line is entered N,m,k,s (0 < n,m,k,s < 100) four positive integers. Indicates the experience value that is also required. Retention of endurance. The number of strange species and the maximum number of monsters to kill.
Next, enter the K-line data.
Each row of data entered two positive integers a, a, B (0 < b < 20), respectively, to kill a single such a strange XHD will get experience and loss of endurance. (there are countless of them every strange)
OutputThe maximum endurance that the output can retain at this level is assumed to be unable to complete this level of output-1.
Sample Input
10 10 1 101 110 10 1 91 19 10 2 101 12 2
Sample Output
0-11
Solution One: Total backpack ********************************
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include < cstring> #include <cctype> #define INF 0x3f3f3f3f#define maxn 100+10using namespace Std;int cost[maxn],val[maxn ],cnt[maxn];int dp[maxn];int n,m,k,s;void completepack () {memset (cnt,0,sizeof cnt); Memset (Dp,0,sizeof (DP)); for (int i=1;i<=k;i++) for (int j=cost[i];j<=m;j++) {if (Dp[j]<dp[j-cost[i]]+val[i]) {cnt[j]=cnt[j-cost[i]]+1; Count Array Dp[j]=dp[j-cost[i]]+val[i]; }}}int Main () {while (scanf ("%d%d%d%d", &n,&m,&k,&s)!=eof) {int ok=1; for (int i=1;i<=k;i++) scanf ("%d%d", val+i,cost+i); Completepack (); for (int i=0;i<=m;i++)//here seems to be the title of the bug, experience can be 0. {if (dp[i]>=n&&cnt[i]<=s) {printf ("%d\n", m-i); ok=0; BrEak }} if (OK) printf (" -1\n"); } return 0;}
Solution 1:2 Heavy Cost backpack ********************************
The code might-----------
#include <iostream> #include <cstdio> #include <cstring> #define MAXN 100+10using namespace Std;int Cost[maxn],val[maxn],num[maxn],dp[maxn][maxn];int n,m,k,s;void Twocost () { memset (dp,0,sizeof dp); for (int i=1;i<=k;i++) for (int t=1;t<=s;t++) for (int j=cost[i];j<=m;j++) { Dp[j][t]=max ( Dp[j][t],dp[j-cost[i]][t-1]+val[i]);} } int main () { int minn; while (scanf ("%d%d%d%d", &n,&m,&k,&s)! = EOF) {for (int i = 1; I <= K; i++) scanf ("%d%d",& Val[i],&cost[i]); Twocost (); if (Dp[m][s] < n) printf (" -1\n"), Else{minn = m;for (int i = 0; I <= s; i++) {for (int j = 0; J <=m; J + +) if (Dp[j][i] &G T;= N&&J < MINN) Minn = j;} printf ("%d\n", M-minn);}}}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
HDU 2159 Fate (Full backpack + limited time) () Double charge backpack