Come on, get a backpack, open an appetizer Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title Description
There are n kinds of goods, each commodity has Si pieces, the price and quality of each commodity are Pi and Mirespectively. Now you have the money. V, ask what the total mass of the goods you can buy is the largest.
Enter multiple sets of inputs. For each set of inputs:
The first line is two integers n,V(1 <= n <= 3,1 <= V <= 1,000,000,000).
The next n rows, three integers per line, Si,Pi,Mi, respectively, represent the first i quantity, price and quality of the items (1 <= Si <= 100,1 <= Pi <= 1,000,000,000, 1 <= Mi <= 1,000,000,000) .
Output for each set of data, output an integer representing the answer. Sample input
1 1010 1 10
Sample output
100
Tip This question number is too big, can not use backpack dp[10000000 ...], even with the map, also because from 0 initialized to 1 billion and timeout;
But the quantity is small, can be violent, can DFS.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath>using namespace std; int main () {int n;long long v,sum=-1,ans,s[4],m[5],p[5];while (~scanf ("%d%lld", &n,&v)) {Sum=-1;memset (s,0, sizeof (s)); memset (P,0,sizeof (P)); memset (M,0,sizeof (m)); for (int i=0;i<n;i++) scanf ("%lld%lld%lld", &s[i], &p[i],&m[i]); for (int i=0;i<=s[0];i++)
#include <iostream> #include <cstring> #include <cstdio>using namespace std;long long int n,v;long long Sum=-1;long long int m[1000],p[1000],s[1000];int vis[5],tt=0;int DFS (int i,long long int v,long long int ans,int num) {
if (v<0) return 0; if (i>n) //type return 0; if (NUM>TT) //number cannot exceed the total number of return 0; if (ans>sum) { Sum=ans; return 0; } for (int j=0; j<=s[i]; j + +) { if (v-p[i]*j>=0) { DFS (i+1,v-p[i]*j,ans+m[i]*j,num+j); } }}int Main () {while (~scanf ("%lld%lld", &n,&v)) { tt=0; for (int i=0; i<n; i++) { scanf ("%lld%lld%lld", &s[i],&p[i],&m[i]); Tt+=s[i]; } memset (vis,0,sizeof (Vis)); sum=0; DFS (0,v,0,0); printf ("%lld\n", sum);} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SDUT3303 to send a backpack open appetizer (simulated backpack), DFS